.net WCF 调用服务失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7103311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:49:58  来源:igfitidea点击:

WCF invoking service fail

.netwcfiis

提问by Khant Soe Htet

I was able to call it in my own server and it works but the problem occurs when I host wcf service in IIS. The details of this error are as follows...

我能够在我自己的服务器中调用它并且它可以工作,但是当我在 IIS 中托管 wcf 服务时会出现问题。这个错误的详细信息如下...

Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.

Stack Trace Error Details

The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug>configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.

Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at IUserService.GetUser(String UserID)
at UserServiceClient.GetUser(String UserID)

调用服务失败。可能原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。有关更多详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理、恢复到默认配置或刷新服务来恢复。

堆栈跟踪错误详细信息

由于内部错误,服务器无法处理请求。有关错误的详细信息,请在服务器上打开 IncludeExceptionDetailInFaults(从 ServiceBehaviorAttribute 或从<serviceDebug>配置行为)以将异常信息发送回客户端,或者根据 Microsoft .NET Framework 3.0 SDK 文档打开跟踪并检查服务器跟踪日志。

服务器堆栈跟踪:
位于 System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System. ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System. ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

在 [0]
处抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at IUserService.GetUser( String UserID)
在 UserServiceClient.GetUser(String UserID)

Here are the source codes:

以下是源代码:

  1. app.config file from wcfservicelirbrary

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
    <system.web>
    <compilation debug="true" />
    </system.web>
      <system.serviceModel>
        <services>
          <service name="UserServiceLibrary.UserService">
            <endpoint address="" binding="wsHttpBinding"      contract="UserServiceLibrary.IUserService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8732/Design_Time_Addresses/UserServiceLibrary/UserService/" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>        
    </configuration>
    
  2. Web config file from WCF Website Host

    <?xml version="1.0"?>
     <configuration>
    
      <system.web>
        <compilation debug="false" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <services>
          <service name="UserServiceLibrary.UserService">
           <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
      contract="IMetadataExchange" />
           <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration=""
      contract="UserServiceLibrary.IUserService" />
           <endpoint address="" binding="wsHttpBinding" contract="UserServiceLibrary.IUserService" />
          </service>
        </services>
        <behaviors>
         <serviceBehaviors>
           <behavior>
    
             <serviceMetadata httpGetEnabled="true"/>
    
              <serviceDebug includeExceptionDetailInFaults="false"/>
           </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
       <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>  
    </configuration>
    
  1. wcfservicelirbrary 中的 app.config 文件

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
    <system.web>
    <compilation debug="true" />
    </system.web>
      <system.serviceModel>
        <services>
          <service name="UserServiceLibrary.UserService">
            <endpoint address="" binding="wsHttpBinding"      contract="UserServiceLibrary.IUserService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8732/Design_Time_Addresses/UserServiceLibrary/UserService/" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>        
    </configuration>
    
  2. 来自 WCF 网站主机的 Web 配置文件

    <?xml version="1.0"?>
     <configuration>
    
      <system.web>
        <compilation debug="false" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <services>
          <service name="UserServiceLibrary.UserService">
           <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
      contract="IMetadataExchange" />
           <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration=""
      contract="UserServiceLibrary.IUserService" />
           <endpoint address="" binding="wsHttpBinding" contract="UserServiceLibrary.IUserService" />
          </service>
        </services>
        <behaviors>
         <serviceBehaviors>
           <behavior>
    
             <serviceMetadata httpGetEnabled="true"/>
    
              <serviceDebug includeExceptionDetailInFaults="false"/>
           </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
       <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>  
    </configuration>
    

回答by Muhammedh

Ensure that account running your process where the service is hosted has access rights to the database. For example in case of IIS the account running the application pool where the service is hosted must have login to database server and it must have permissions to do all necessary operations in your database.

确保运行托管服务的进程的帐户具有对数据库的访问权限。例如,在 IIS 的情况下,运行托管服务的应用程序池的帐户必须登录到数据库服务器,并且必须具有在数据库中执行所有必要操作的权限。

回答by Jeremy McGee

As usual with WCF errors, the key is most likely right at the bottom of the stack trace:

与 WCF 错误一样,密钥很可能就在堆栈跟踪的底部:

UserServiceClient.GetUser(String UserID)

UserServiceClient.GetUser(String UserID)

This was what caused the error in the first place.

这就是首先导致错误的原因。

To check, if you have access to the server, try attaching a Visual Studio debugger to the IIS service (usually w3wp.exe) that hosts your WCF code. (Do this though Debug | Attach to Process...; you'll need be running with Administrator privileges, and check "on" the "View processes from all users" option).

要检查您是否有权访问服务器,请尝试将 Visual Studio 调试器附加到w3wp.exe承载 WCF 代码的 IIS 服务(通常为)。(通过 Debug | Attach to Process... 执行此操作;您需要以管理员权限运行,并选中“打开”“查看所有用户的进程”选项)。

If you don't have direct access to attach a debugger to the service but can tweak the configuration file for the service, then try the WCF diagnostic traces (documented on MSDN here). This lets you create logs that can be viewed with the Service Trace Viewer - you should be able to find much more information on the exception very easily using this.

如果您无法直接访问将调试器附加到服务但可以调整服务的配置文件,请尝试 WCF 诊断跟踪(在 MSDN此处记录)。这使您可以创建可以使用 Service Trace Viewer 查看的日志 - 您应该能够使用它非常轻松地找到有关异常的更多信息。

A hunch: is UserIDnull, by any chance? If so, and you're not expecting it to be, has the service contract changed recently? Adding System.Runtime.Serializationto the diagnostic trace for the Service Viewer can bring up some more information on that: details here.

预感:是否为UserID空?如果是这样,而且您并不期望它是这样,服务合同最近是否发生了变化?添加System.Runtime.Serialization到 Service Viewer 的诊断跟踪中可以显示更多相关信息:详情请点击此处

回答by HydTechie

Best practices with WCF,

WCF 的最佳实践,

  1. Make sure WCF project url along with Virtual directory is showing up without errors, and could generate proxy ( svcutil followed by hyperlink)
  1. 确保 WCF 项目 url 和虚拟目录显示没有错误,并且可以生成代理( svcutil 后跟超链接)

2 Once WCF is fine, in Client project, In ServiceReferences, choose CONFIGURE services with this above URL, 3. That should generate proxy, if its attached to source control like TFS, please make sure to check in - delete mode checkins also, ALONG with newly generated files ( show hidden files in the project)

2 WCF 正常后,在 Client 项目中,在 ServiceReferences 中,选择 CONFIGURE services with this above URL, 3. 那应该生成代理,如果它附加到像 TFS 这样的源代码管理,请确保签入 - 删除模式签入也,ALONG使用新生成的文件(显示项目中的隐藏文件)

  1. Click on Reference.cs --> do a acid test to verify your new datacontract or datamember fields are generated, that confirms service is Upto Date
  1. 单击 Reference.cs --> 进行酸性测试以验证生成了新的数据合同或数据成员字段,以确认服务是最新的

All those nasty WCF references error tough to solve, but regenerating may give relief some times...

所有那些讨厌的 WCF 引用错误都很难解决,但有时重新生成可能会有所缓解......