2010-09-27 109 views
0

我有一個名爲myService.svc的服務。該服務使用webHttpBinding綁定進行公開。原因是因爲我需要能夠從iPhone和JQuery界面調用服務。據我瞭解,WP7只支持依賴SOAP消息的BasicHttpBinding。不幸的是,SOAP不直接支持iPhone。與通過HTTP請求公開的服務進行交互

我的問題是,我不能使用WebClient或HttpWebRequest與使用webHttpBinding的服務進行交互。從概念上講,我相信它會起作用,但實施方式有點暗指我。我已經定義了以下服務:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
[ServiceBehavior(IncludeExceptionDetailInFaults = false)] 
public class myService : ImyService 
{ 
    public MyServiceResult MyMethod(string p1, string p2) 
    { 
    try 
    { 
     // Do stuff 
     MyResponseObject r = new MyResponseObject(); 
     r.Property1 = DateTime.Now; 
     r.Property2 = "Some other data"; 
     return r; 
    } 
    catch (Exception ex) 
    { 
     return null; 
    } 
    } 
} 

[ServiceContract] 
public interface ImyService 
{ 
    [OperationContract] 
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)] 
    MyServiceResult MyMethod(string p1, string p2); 
} 

從我的WP7的應用程序,我使用下面的代碼:

string opUrl = "http://localhost:80/services/myService.svc"; 
Uri myServiceUri = new Uri(opUrl, UriKind.Absolute); 

WebClient myServiceClient = new WebClient(); 
myServiceClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myServiceClient_DownloadStringCompleted); 
myServiceClient.DownloadStringAsync(myServiceUri); 

private void myServiceClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
    if (e.Result == null) 
    MessageBox.Show("null"); 
    else 
    MessageBox.Show(e.Result); 
} 

當我執行此,e.Result不爲空。而是,返回一堆HTML告訴我使用svcutil.exe。我的問題是:1)如何使用webHttpBinding將這個「MyMethod」稱爲? 2)我如何傳入參數?

回答

1

不要必須使用服務綁定。您是而不僅限於SOAP格式的消息。

您可以使用WebClientHttpWebRequest

請注意,雖然手機只支持異步請求。