Interacting with a service exposed through HTTP Request
I h开发者_StackOverflow社区ave a service called myService.svc. This service is exposed using a webHttpBinding binding. The reason why is because I need to be able to call the service from an iPhone and a JQuery interface as well. From my understanding, WP7 only directly supports BasicHttpBinding which relies on SOAP messages. Unfortunately, SOAP is not directly supported on the iPhone.
My question is, can't I use a WebClient or HttpWebRequest to interact with a service that uses webHttpBinding. Conceptually I believe it would work, but the implementation is sort of alluding me. I have the following service defined:
[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);
}
From my WP7 app, I am using the following code:
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);
}
When I execute this, e.Result is not null. Rather, a bunch of HTML is returned telling me to use svcutil.exe. My questions are: 1) how do I call this "MyMethod" using webHttpBinding? 2) how do I pass in parameters?
You do not have to use a service binding. You are not tied to SOAP format messages only.
You can use WebClient and HttpWebRequest.
Note that only asynchronous requests are supported on the phone though.
精彩评论