如何为WCF Rest服务配置web.config?

时间:2014-06-18 11:58:01

标签: c# wcf rest web-config wcf-binding

我正在创建一个WCF Rest网站,但无法弄清楚如何配置web.config。我在网上看到了一些例子,但他们都是非休息的。除了绑定之外,我不知道它是否与配置有任何区别,因为这是我第一次使用WCF Rest。

我将WCF站点部署到我的IIS服务器,当我转到svc.myproj.com/AccountService.svc时,我可以访问该默认WCF页面。我甚至可以使用svcutil来使用相同的url生成客户端,但每当我尝试在客户端应用程序中连接到该服务时,我都会发现端点未找到异常。

有什么想法吗?

这是我的AccountService.svc:

[ServiceContract]
public class AccountService {

        [OperationContract]
        public bool IsRegisteredUser(string emailOrUsername)
        {
            return this.GetUser(emailOrUsername) != null;
        }
}

这是我的web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MyProj.Services.AccountService" behaviorConfiguration="ServiceBehavior">
        <endpoint address="/AccountService.svc" behaviorConfiguration="MyProj.Services.AccountServiceAspNetAjaxBehavior"
          binding="webHttpBinding" contract="MyProj.Services.AccountService" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="MyProj.Services.AccountServiceAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
    <validation validateIntegratedModeConfiguration="false"/>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试使用<enableWebScript />来查看行为标记内是否有帮助,并尝试更改IsRegisteredUser的装饰器,而不是<webHttp/>

[OperationContract]
[WebInvoke(UriTemplate = "/IsRegisteredUser", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", ResponseFormat = WebMessageFormat.Json)]
public bool IsRegisteredUser(string emailOrUsername)
{
  return this.GetUser(emailOrUsername) != null;
}