WCF调用服务失败

4
我能在自己的服务器上调用它并且它可以正常工作,但是当我将wcf服务托管在IIS中时就会出现问题。错误的详细信息如下...
Failed to invoke the service.可能的原因:服务离线或不可访问;客户端配置与代理不匹配;现有代理无效。请参考堆栈跟踪以获取更多详细信息。您可以尝试通过启动新的代理、恢复默认配置或刷新服务来恢复。
堆栈跟踪错误详细信息
由于内部错误,服务器无法处理请求。有关错误的更多信息,请在服务器上启用IncludeExceptionDetailInFaults(从ServiceBehaviorAttribute或配置行为)以将异常信息发送回客户端,或按照Microsoft .NET Framework 3.0 SDK文档中的说明开启跟踪并检查服务器跟踪日志。
服务器堆栈跟踪: 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)
异常被重新抛出[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)
以下是源代码:
  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>
    

2
首先,转到服务器并应用以下配置更新:http://msdn.microsoft.com/zh-cn/library/system.servicemodel.servicebehaviorattribute.includeexceptiondetailinfaults.aspx - dotnetnate
还要看一下 UserServiceClient.GetUser(String UserID) - 这就是异常最初发生的地方。发布你的代码和从dotnetnate的建议中获得的任何其他信息,以获得更好的答案 :) - Tim
以下是我配置的源代码,请检查一下。谢谢。 - user851449
4个回答

2

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


从 http://stackoverflow.com/questions/6594001/access-denied-error-for-a-method-in-web-service 复制了答案。 - Muhammedh

1

通常在 WCF 错误中,关键信息很可能就在堆栈跟踪的底部:

UserServiceClient.GetUser(String UserID)

这就是导致错误的原因。

如果您可以访问服务器,请尝试将 Visual Studio 调试器附加到托管 WCF 代码的 IIS 服务(通常为 w3wp.exe)。(通过“调试”|“附加到进程…”进行操作;您需要以管理员权限运行,并选中“查看所有用户的进程”选项)。

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

一种预感:你有没有偶然发现 UserID 是 null 的?如果是,而你并不期望它是这样的,服务契约最近是否有所改变?将 System.Runtime.Serialization 添加到服务查看器的诊断跟踪中可以提供更多有关其详细信息的信息:详情请参见此处

根据服务跟踪查看器,我得到了以下错误消息:
  1. 无法读取消息正文,因为它为空。
  2. 没有通道可以接受带有操作“http://schemas.xmlsoap.org/ws/2004/09/transfer/Get”的消息。
  3. 用户“IIS APPPOOL\ASP.NET v4.0”的登录失败。
- user851449
2
啊,最后一个听起来像是你的问题;我猜测你的服务器上IIS配置不正确。其他ASP.NET 4应用程序是否正常工作?如果是这样,请检查服务的应用程序池的凭据,因为它似乎无法初始化,因为其凭据不正确。 - Jeremy McGee
@JeremyMcGee,你说得对。是我在应用程序池上的用户权限问题。谢谢 :) - Muhammedh

0

WCF 的最佳实践:

  1. 确保 WCF 项目的 URL 和虚拟目录没有错误,并且可以生成代理(使用 svcutil 后跟超链接)。

2. 一旦 WCF 正常运行,在客户端项目中,在 ServiceReferences 中选择“使用上述 URL 配置服务”, 3. 这应该会生成代理,如果它附加到源代码控制(如 TFS),请确保检入 - 删除模式检入以及新生成的文件(在项目中显示隐藏文件)。

  1. 单击 Reference.cs --> 进行酸性测试以验证是否生成了新的数据合同或数据成员字段,这将确认服务已经更新。

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


0

问题:- 我们的一个Windows服务器上托管的WCF服务出现了同样的问题。应用程序无法连接到服务。

解决方案:- 必须重置WCF服务的应用程序池。

谢谢


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接