【问题标题】:Error referencing WCF service that I created引用我创建的 WCF 服务时出错
【发布时间】:2021-01-03 15:41:43
【问题描述】:

我对 WCF 还很陌生,所以我在 web.config 上苦苦挣扎。

我在 IIS10 上创建了 WCF 服务。这是我的 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>
  <system.web>
    <customErrors mode="Off" />
    <compilation targetFramework="4.0" debug="true">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="DefaultCache" duration="60" varyByParam="none" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
    <authentication mode="Windows" />
  </system.web>

  <system.serviceModel>
    <services>
      <service name="FileStorageService.FileService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfiguration" contract="FileStorageService.IFileService" />

        <!--<endpoint address="mex"  
                  binding="mexHttpsBinding"  
                  contract="IMetadataExchange" />-->
      </service>
    </services>

    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingConfiguration">
          <security mode="Transport">
            <transport clientCredentialType="Basic" proxyCredentialType="None" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
          <!-- 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="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>


  <system.webServer>
    <httpErrors errorMode="Detailed" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <connectionStrings>
  </connectionStrings>
  <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>
</configuration>

我可以引用该 api,但是当我尝试使用它时收到此消息:

服务器堆栈跟踪:在 System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(消息 回复、MessageFault 故障、字符串操作、MessageVersion 版本、 故障转换器故障转换器)在 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime 操作,ProxyRpc& rpc) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串动作, Boolean oneway, ProxyOperationRuntime 操作, Object[] ins, Object[] 出局,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime 操作)在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 留言)

在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(消息数据& msgData,Int32 类型)在 PpsTransactionFile.MyFileServiceReference.IFileService.DownloadPpsZipFile(DownloadFileRequest 下载文件请求)在 PpsTransactionFile.MyFileServiceReference.FileServiceClient.DownloadPpsZipFile(DownloadFileRequest downloadFileRequest) 在 D:\Source\Sub\Web 项目\WebStorageApps\PpsTransactionFile\Service References\MyFileServiceReference\Reference.cs:第 493 行 PpsTransactionFile.download.DownloadFile.DownloadPpsZipFile(FileParms fileParms) 在 D:\Source\Sub\Web 项目\WebStorageApps\PpsTransactionFile\download\DownloadFile.cs:line 155

我的代码:

public static Status DownloadPpsTransactionFile(FileParms fileParms)
{
  var client = new FileServiceClient();

  try
  {
    var downloadstatus = new Status();
    downloadstatus.ProgramSendType = fileParms.SendGet;

    client.ClientCredentials.UserName.UserName = "xxxxxx";
    client.ClientCredentials.UserName.Password = "xxxxxxxxxxx";
    var trucks = fileParms.Trucks.Split(Convert.ToChar(","));

    foreach (var truck in trucks)
    {
      int trk;
      if (!int.TryParse(truck, out trk))
      {
        continue;
      }

      var downloadrequest = new DownloadFileRequest();
      downloadrequest.CompanyCode = fileParms.CompanyCode;
      downloadrequest.FileType = 1;
      downloadrequest.TruckNumber = trk;
      DownloadFileResponse download;

      do
      {
        download = client.DownloadPpsTransactionFile(downloadrequest);

当我调用 wfc 方法时,它在最后一行失败。我似乎无法让 IIS 设置和 web.config 正常工作。

我在 IIS 上的设置:

我做错了什么?

客户端配置

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IFileService">
          <security mode="Transport">
            <transport clientCredentialType="Basic" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://SOMEDOMAIN.COM/api/fileservice/FileService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileService"
        contract="MyFileServiceReference.IFileService" name="BasicHttpBinding_IFileService" />
    </client>
  </system.serviceModel>

</configuration>

附加信息

我在事件查看器中找到了这个。我目前正在研究它以了解它的含义。

FailedRequestTracing 模块未能将缓冲事件写入日志 匹配失败定义的请求的文件。不会有日志 生成,直到这种情况得到纠正。问题发生在 在最后 5 分钟内至少 1 次。数据就是错误。

【问题讨论】:

  • 异常信息是什么?另外,服务器是否暴露在 ssl 上?
  • 什么是客户端 WCF 配置?如果您有一个 C# 客户端 - 那是一个带有 app.config 的命令行应用程序 - 那是什么样的?它是一个带有web.config 的网络应用程序 - 那么它是什么样的?您必须在客户端具有“相同”配置才能与您在服务器上定义的 WCF 服务通信。
  • @WiktorZychla 谢谢回复。如果您要问的话,我已经安装了 ssl 证书?
  • @marc_s 我已经发布了客户端配置。
  • @marc_s 这是一个连接到 IIS10 托管的 WCF 服务的 winforms 应用程序。我在上面发布了网络和应用配置,这是否回答了您的问题?

标签: c# wcf iis-10


【解决方案1】:

这不是我的答案,但我不能将此作为评论发布。这是一种更简洁的方式来显示异常,它会在D:\Source\Sub\Web Projects\WebStorageApps\PpsTransactionFile\download\DownloadFile.cs:line 155 中为您提供准确的行号。它还表明异常已 rethrown 暗示故障已被理解,因此您可以在那里开始调查:

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 PpsTransactionFile.MyFileServiceReference.IFileService.DownloadPpsZipFile(DownloadFileRequest downloadFileRequest)

at PpsTransactionFile.MyFileServiceReference.FileServiceClient.DownloadPpsZipFile(DownloadFileRequest downloadFileRequest)
in D:\Source\Sub\Web Projects\WebStorageApps\PpsTransactionFile\Service References\MyFileServiceReference\Reference.cs:line 493

at PpsTransactionFile.download.DownloadFile.DownloadPpsZipFile(FileParms fileParms)
in D:\Source\Sub\Web Projects\WebStorageApps\PpsTransactionFile\download\DownloadFile.cs:line 155

也许这只是一个错字,因为我注意到您在客户端端点配置中拼写错误:&lt;endpoint address="https://SOMEDOMIAN.COM ...,我猜它应该是&lt;endpoint address="https://SOMEDOMAIN.COM...(注意 DOMAIN 中的“IA”而不是“AI”)。

【讨论】:

  • 感谢您的意见。我发现我添加到我的帖子中的另一个错误。
【解决方案2】:

我无法将其作为评论发布,但我建议您在堆栈跟踪中找到异常消息。它可以说明响应没有被反序列化的原因。

如果这没有帮助,我会尝试使用 Wireshark 或日志代理来跟踪并查看服务器回复您的请求。

【讨论】:

    【解决方案3】:

    您似乎一直在遵循本指南:

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl

    如果您按照他们的示例进行设置,您的代码是否可以正常工作?

    <?xml version="1.0"?>  
    <configuration>  
      
      <system.web>  
        <compilation debug="true" targetFramework="4.0" />  
      </system.web>  
      <system.serviceModel>  
        <services>  
          <service name="MySecureWCFService.Service1">  
            <endpoint address=""  
                      binding="basicHttpBinding"  
                      bindingConfiguration="secureHttpBinding"  
                      contract="MySecureWCFService.IService1"/>  
      
            <endpoint address="mex"  
                      binding="mexHttpsBinding"  
                      contract="IMetadataExchange" />  
          </service>  
        </services>  
        <bindings>  
          <basicHttpBinding>  
            <binding name="secureHttpBinding">  
              <security mode="Transport">  
                <transport clientCredentialType="None"/>  
              </security>  
            </binding>  
          </basicHttpBinding>  
        </bindings>  
        <behaviors>  
          <serviceBehaviors>  
            <behavior>  
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->  
              <serviceMetadata 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>  
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />  
      </system.serviceModel>  
      <system.webServer>  
        <modules runAllManagedModulesForAllRequests="true"/>  
      </system.webServer>  
      
    </configuration>
    

    鉴于错误:

    FailedRequestTracing 模块未能将缓冲事件写入日志 匹配失败定义的请求的文件。不会有日志 生成,直到这种情况得到纠正。问题发生在 在最后 5 分钟内至少 1 次。数据就是错误。

    在这里查看配置:

    https://docs.microsoft.com/en-us/iis/troubleshoot/using-failed-request-tracing/troubleshooting-failed-requests-using-tracing-in-iis-85

    我还建议您从简单的开始:创建一个可以工作的基本 WCF Web HTTP 服务,然后进行修改。如果您还没有尝试过,这可能会容易得多。

    控制台示例:

    Service.cs:

    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace Microsoft.ServiceModel.Samples.BasicWebProgramming
    {
        [ServiceContract]
        public interface IService
        {
            [OperationContract]
            [WebGet]
            string EchoWithGet(string s);
    
            [OperationContract]
            [WebInvoke]
            string EchoWithPost(string s);
        }
        public class Service : IService
        {
            public string EchoWithGet(string s)
            {
                return "You said " + s;
            }
    
            public string EchoWithPost(string s)
            {
                return "You said " + s;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/"));
                try
                {
                    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
                    host.Open();
                    using (ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000"))
                    {
                        cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
    
                        IService channel = cf.CreateChannel();
    
                        string s;
    
                        Console.WriteLine("Calling EchoWithGet via HTTP GET: ");
                        s = channel.EchoWithGet("Hello, world");
                        Console.WriteLine("   Output: {0}", s);
    
                        Console.WriteLine("");
                        Console.WriteLine("This can also be accomplished by navigating to");
                        Console.WriteLine("http://localhost:8000/EchoWithGet?s=Hello, world!");
                        Console.WriteLine("in a web browser while this sample is running.");
    
                        Console.WriteLine("");
    
                        Console.WriteLine("Calling EchoWithPost via HTTP POST: ");
                        s = channel.EchoWithPost("Hello, world");
                        Console.WriteLine("   Output: {0}", s);
                        Console.WriteLine("");
                    }
    
                    Console.WriteLine("Press <ENTER> to terminate");
                    Console.ReadLine();
    
                    host.Close();
                }
                catch (CommunicationException cex)
                {
                    Console.WriteLine("An exception occurred: {0}", cex.Message);
                    host.Abort();
                }
            }
        }
    }
    

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-a-basic-wcf-web-http-service

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 2012-02-07
      • 2015-03-10
      • 2011-08-27
      相关资源
      最近更新 更多