[英]How to configure web.config for WCF Rest Service?
我正在創建一個 WCF Rest 網站,但不太清楚如何配置 web.config。 我在網上看到了一些例子,但它們都是非 Rest 的。 我不知道除了綁定之外在配置方面是否有什么不同,因為這是我第一次使用 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>
謝謝!
您可以嘗試使用<webHttp/>
而不是<enableWebScript />
來查看行為標簽內部是否有幫助,並嘗試更改 IsRegisteredUser 的裝飾器
[OperationContract]
[WebInvoke(UriTemplate = "/IsRegisteredUser", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", ResponseFormat = WebMessageFormat.Json)]
public bool IsRegisteredUser(string emailOrUsername)
{
return this.GetUser(emailOrUsername) != null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.