与通过HTTP请求公开的服务交互

时间:2010-09-27 19:16:09

标签: wcf windows-phone-7

我有一个名为myService.svc的服务。使用webHttpBinding绑定公开此服务。原因是因为我需要能够从iPhone和JQuery接口调用服务。根据我的理解,WP7只直接支持依赖于SOAP消息的BasicHttpBinding。不幸的是,iPhone不直接支持SOAP。

我的问题是,我不能使用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不为null。相反,返回一堆HTML告诉我使用svcutil.exe。我的问题是:1)如何使用webHttpBinding调用此“MyMethod”? 2)如何传递参数?

1 个答案:

答案 0 :(得分:1)

必须使用服务绑定。您仅与SOAP 格式消息无关。

您可以使用WebClientHttpWebRequest

请注意,手机仅支持异步请求。

相关问题