我有一个名为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)如何传递参数?