我想在Visual Studio 2013应用程序中创建一个Web服务。
我收到以下错误:“mscorlib.dll中发生了'System.ServiceModel.ProtocolException'类型的异常,但未在用户代码中处理”。
WCF服务Web.Config
:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8095/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
<!--
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"/>
</system.webServer>
</configuration>
网站项目服务客户Web.Config
:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.1"/>
<httpRuntime targetFramework="4.5.1"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8095/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
答案 0 :(得分:1)
看起来你有很多东西被复制粘贴到错误的地方。
至少,您的WCF服务配置的system.ServiceModel部分看起来应该更像......
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
</bindings>
<services>
<service name="Service1">
<endpoint address="Service1.svc" name="Service1Endpoint"
binding="basicHttpBinding" contract="ServiceReference1.IService1" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8095/Service1.svc" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
尝试将配置调整为此,然后使用网络浏览器查看网址。如果您看到允许生成WSDL的页面,那么您只需要创建匹配的客户端绑定。
客户端配置的一个例子就是这样......
<system.serviceModel>
<behaviors>
</behaviors>
<bindings>
</bindings>
<client>
<endpoint address="http://localhost:8095/Service1.svc"
binding="basicHttpBinding" contract="ServiceReference1.IService1"
name="MyServiceClient" />
</client>
</system.serviceModel>
然后要创建代码内客户端,您的代码看起来就像这样......
var factory = new ChannelFactory<IService1>("MyServiceClient");
var channel = factory.CreateChannel();
由于所有潜在的设置组合,WCF配置有时会很痛苦。通常需要进行一些调整才能使其以您期望的方式工作。