C# Windows 10应用程序中的WCF服务

C# Windows 10应用程序中的WCF服务,c#,wcf,iis,uwp,C#,Wcf,Iis,Uwp,我创建了一个WCF服务,它从数据库检索数据并显示信息 第一个问题:当我向商店提交应用程序时,WCF服务是否与应用程序捆绑在一起,这是如何工作的 第二个问题:我注意到当服务运行时,IIS也在我的系统上运行,那么如果用户没有IIS会发生什么,或者当它在windows phone上运行时会发生什么 最后,我注意到当IIS没有运行并且我打开应用程序时,应用程序崩溃了,为什么它会这样做,它不能启动服务吗 请注意,我不是这方面的专家,这是我第一次使用WCF服务,所以请耐心等待我,并尽可能多地提供详细信息 谢

我创建了一个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;
    }
}
}

}

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:VisualStudio为您启动IIS,但在生产环境中,它应该托管在服务器上。Windows Phone应用程序通常通过internet连接到它。一个托管选项是Azure

问题3:如果服务不可用,应用程序应该处理它…(例如,按钮单击事件处理程序中的try catch将完成此工作…)

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;
    }
}