2015-10-20 13 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事件處理程序嘗試捕捉會做的工作。)

相關問題