开发者

Exposing WCF Service to Mobile Clients

I have an existing ASP.NET web application. This ASP.NET web application uses JQuery to provide a rich experience to the users. This user interface interacts with the server through some WCF services. A sample service looks like the following:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults = false)]
public class myService : ImyService
{
  public bool SomeMethod(string parameter1, string parameter2)
  {
    try
    {
      return true;
    }
    catch (Exception ex)
    {
      return false;
    }
  }
}

I now want to expose this service to an iPhone and a Windows Phone 7 application. In an attempt to do this, I have configured the service like the following:

<system.serviceModel>      
  <behaviors>
    <endpointBehaviors>
      <behavior name="myServiceBehavior">
        <enableWebScript />
      </behavior>
    </endpoin开发者_高级运维tBehaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>

  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  <services>
    <service name="myService">
      <endpoint address="" behaviorConfiguration="myServiceBehavior"
        binding="webHttpBinding" contract="ImyService" />
    </service>
  </services>
</system.serviceModel>

The service works with the JQuery calls in my ASP.NET web application. I have not begun working on the iPhone client. But, when I try to expose this service to my WP7 client, I run into problems. As it stands now, when I launch my WP7 application, I receive an error that says:

KeyNotFoundException

If I change the binding in the config file to "basicHttpBinding", I cannot reference the service in Visual Studio. I receive an error that says:

The endpoint at 'http://machine:80/services/myService.svc' does not have a Binding with the None MessageVersion.  'System.ServiceModel.Description.WebScriptEnablingBehavior' is only intended for use with WebHttpBinding or similar bindings.

Ugh. How do I move forward? I thought WCF was designed to make this stuff easier. But I feel like I'm getting stuck doing something relatively basic.

Thank you for your help!


As far as I know (but I have no experience at all with iPhone or WinPhone development) you should be able to expose your WCF service using the regular webHttpBinding and no web script functionality, to get a normal REST style WCF service:

<system.serviceModel>      
   <services>
      <service name="myService">
         <endpoint 
             address="" 
             binding="webHttpBinding" 
             contract="ImyService" />
      </service>
   </services>
</system.serviceModel>

That alone should be sufficient - just browse to the virtual directory where your *.svc file is located and you should see your top-level resources in your browser.

The mobile devices typically don't support the SOAP-style bindings (like basicHttpBinding and so on) - so you need to use webHttpBinding instead (since that really only requires an HTTP client stack on the device - and every device these days definitely has this!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜