【问题标题】:How to configure web.config for WCF Rest Service?如何为 WCF Rest 服务配置 web.config?
【发布时间】:2014-06-18 11:58:00
【问题描述】:

我正在创建一个 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>

谢谢!

【问题讨论】:

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


    【解决方案1】:

    您可以尝试使用&lt;webHttp/&gt; 来代替&lt;enableWebScript /&gt;,看看这是否有助于在行为标签中,并尝试更改 IsRegisteredUser 的装饰器

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-23
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      相关资源
      最近更新 更多