繁体   English   中英

Windows 10应用中的WCF服务

[英]WCF service in Windows 10 app

我创建了WCF服务,该服务从数据库检索数据并显示信息。

第一个问题:当我将应用程序提交到商店时,WCF服务是否与应用程序捆绑在一起,它如何工作?

第二个问题:我注意到服务在运行时,IIS也在我的系统上运行,所以如果用户没有IIS会发生什么,或者在Windows Phone上运行会发生什么。

最后,我注意到当IIS未运行且我打开该应用程序时,该应用程序崩溃了,为什么这样做了,它不应该能够启动该服务吗?

请我不是这方面的专家,这是我第一次使用WCF服务,所以请耐心等待,并提供尽可能多的细节。

谢谢。

WCF服务:

namespace CustomerService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
    SqlConnection sqlCon = new SqlConnection("Data Source=MOD;Initial Catalog=DB2;User ID=sa;Password=*********");
    public Customer getCustomer()
    {
        try
        {
            sqlCon.Open();
            string strSql = "SELECT * FROM Table_1";
            DataSet ds = new DataSet();
            SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlCon);
            sqlDa.Fill(ds);

            Customer objCus = new Customer();
            objCus.Age = (int)ds.Tables[0].Rows[0][0];
            objCus.Name = ds.Tables[0].Rows[0][1].ToString();
            return objCus;
        }
        catch
        {
            return null;
        }
        finally
        {
            sqlCon.Close();
        }
    }

    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

}

namespace CustomerService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{

    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
    [OperationContract]
    Customer getCustomer();
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

[DataContract]
public class Customer
{
    int age;
    string name;

    [DataMember]
    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    [DataMember]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

}

MainPage.xaml.cs

public sealed partial class MainPage : Page
{
    ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        ServiceReference1.Customer g = await obj.getCustomerAsync();
        ageTB.Text = g.Age.ToString();
        nameTB.Text = g.Name;
    }
}

WCF服务旨在作为后端服务。 这意味着它不随应用程序一起提供,而是托管在其他地方。

所以:问题1:不,它没有捆绑到应用程序中,您必须注意托管

问题2:Visual Studio为您启动了IIS,但在生产中应将其托管在服务器上。 Windows Phone应用程序通常通过互联网连接到它。 一种托管选项是Azure ...

Question3:如果服务不可用,则应用程序应处理该问题……(例如,在Button_Click事件处理程序中尝试try即可完成工作。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM