【问题标题】:"An exception of type 'System.ServiceModel.ProtocolException' occurred in mscorlib.dll but was not handled in user code" error“在 mscorlib.dll 中发生了 'System.ServiceModel.ProtocolException' 类型的异常,但未在用户代码中处理”错误
【发布时间】:2018-01-23 07:04:31
【问题描述】:

我想在 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>

【问题讨论】:

  • 从 system.WebServer 部分中完全删除 bindings 和 client 部分,它们只属于 system.ServiceModel 部分。

标签: c# wcf


【解决方案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 配置有时会很痛苦。通常需要进行一些调整才能使其按您期望的方式工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多