Silverlight 向移动客户端公开WCF服务
我有一个现有的ASP.NET web应用程序。此ASP.NET web应用程序使用JQuery为用户提供丰富的体验。此用户界面通过一些WCF服务与服务器交互。示例服务如下所示:Silverlight 向移动客户端公开WCF服务,silverlight,wcf,Silverlight,Wcf,我有一个现有的ASP.NET web应用程序。此ASP.NET web应用程序使用JQuery为用户提供丰富的体验。此用户界面通过一些WCF服务与服务器交互。示例服务如下所示: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] [ServiceBehavior(IncludeExceptionDetailInFaults = false)] publ
[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;
}
}
}
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="myServiceBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<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>
我现在想将此服务公开给iPhone和WindowsPhone7应用程序。为了做到这一点,我将服务配置如下:
[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;
}
}
}
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="myServiceBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<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>
如果将配置文件中的绑定更改为“basicHttpBinding”,则无法在Visual Studio中引用该服务。我收到一个错误,上面写着:
KeyNotFoundException
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.
啊。我该如何前进?我认为WCF的设计是为了让这些东西更容易。但我觉得我在做一些相对基本的事情
谢谢你的帮助 据我所知(但我对iPhone或WinPhone开发毫无经验),您应该能够使用常规的webHttpBinding
公开您的WCF服务,而不使用web脚本功能,以获得正常的REST样式的WCF服务:
<system.serviceModel>
<services>
<service name="myService">
<endpoint
address=""
binding="webHttpBinding"
contract="ImyService" />
</service>
</services>
</system.serviceModel>
仅此一项就足够了—只需浏览到*.svc文件所在的虚拟目录,您就可以在浏览器中看到顶级资源
移动设备通常不支持SOAP风格的绑定(如basicHttpBinding
等等),因此您需要使用webHttpBinding
(因为这实际上只需要设备上的HTTP客户端堆栈,而现在的每一个设备都肯定有这个!)