向web移动客户端公开webHttpBinding WCF服务

时间:2010-09-26 23:47:03

标签: silverlight wcf windows-phone-7

我创建了一个非常基本的服务操作,需要将内容写入我的数据库。此服务如下所示:

[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;
    }
  }
}

ImyService的定义如下所示:

[ServiceContract]
public interface ImyService
{
  [OperationContract]
  [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
  MyServiceResult MyMethod(string p1, string p2);
}

此服务将暴露给WP7和iPhone客户端应用程序。因此,我相信我需要使用webHttpBinding。这导致我在web.config文件中使用以下设置:

<system.serviceModel>      
  <behaviors>
    <endpointBehaviors>
      <behavior name="myServiceBehavior">
        <webHttp />
      </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>

服务和WP7应用程序都是同一解决方案的一部分。我可以在我的应用程序中成功添加对服务的引用。当我运行应用程序时,引用该服务的页面会引发错误。错误说:

无法在ServiceModel客户端配置部分中找到引用合同“MyServiceProxy.ImyService”的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。

我做错了什么?看起来这应该是一个非常简单的事情。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您是否已将“ServiceReferences.ClientConfig”文件复制到Windows Phone 7项目中?此文件位于WCF项目中。此外,WP7客户端仅支持basicHttpBinding。因此,您可能会看到一个空的“ServiceReferences.ClientConfig”文件,除非您切换到basicHttpBinding

相关问题