2015-10-20 69 views
0

我创建了一个WCF服务,用于从数据库检索数据并显示信息。Windows 10应用程序中的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; 
    } 
} 

回答

0

WCF服务是为了将后端服务。这意味着它不随应用程序一起提供,但它托管在别的地方。

所以: 问题1:不,它不是捆绑的应用程序,你必须要对托管

问题2护理:Visual Studio中启动一个IIS你,但在生产中应该对被托管服务器。 Windows Phone应用通常通过互联网连接到它。一个托管的办法是天青......

问题3:如果服务不可用的应用程序应该处理它?(例如,在Button_Click事件处理程序尝试捕捉会做的工作。)

相关问题