Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试添加服务引用会在一个项目中导致错误请求(400),否则运行正常_C#_Wcf_Windows Services - Fatal编程技术网

C# 尝试添加服务引用会在一个项目中导致错误请求(400),否则运行正常

C# 尝试添加服务引用会在一个项目中导致错误请求(400),否则运行正常,c#,wcf,windows-services,C#,Wcf,Windows Services,我处于一个微妙的境地:正如标题所示,我似乎无法连接到我在Windows服务中打包的WCF服务。我遵循教程的每一步,并让它以这种精确的方式工作多次,只是不适用于一个特定的项目。我真的不知道它是什么,我有一个非常简单的接口,只有一个方法作为合同,我的服务安装得很好,也启动得很好。一旦我尝试在另一个项目中添加服务引用,就会出现错误400、错误请求和元数据问题。 我甚至重写了原型项目(它基本上是同一个项目,在合同的执行过程中代码更少;但我们谈论的代码仍然少于300行),它运行得非常好,并且出现了相同的错

我处于一个微妙的境地:正如标题所示,我似乎无法连接到我在Windows服务中打包的WCF服务。我遵循教程的每一步,并让它以这种精确的方式工作多次,只是不适用于一个特定的项目。我真的不知道它是什么,我有一个非常简单的接口,只有一个方法作为合同,我的服务安装得很好,也启动得很好。一旦我尝试在另一个项目中添加服务引用,就会出现错误400、错误请求和元数据问题。 我甚至重写了原型项目(它基本上是同一个项目,在合同的执行过程中代码更少;但我们谈论的代码仍然少于300行),它运行得非常好,并且出现了相同的错误。当我这样做的时候,我没有更改任何
app.config
代码,我可以在之前连接,但之后的请求不正确

问题的另一个方面是我不能在这里发布任何代码(我在一个虚拟机上工作,在那里我没有互联网接入,加上我在物理机器上的实际互联网接入受到限制,我无法打开任何类型的板/论坛/博客/任何东西,所以我不能发布确切的错误)。因为我所有的最小示例都可以工作,所以我的“最小不工作示例”将是全部代码

我在这里完全不知所措。我深入研究了所有其他关于错误请求错误的主题,明天还有更多的检查要做,但我想我还是在这里发布,也许可以得到一些关于如何进一步测试我的项目错误的建议。 为了以防万一,除了服务和合同名称外,
app.config
与以下内容相同:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>    <services>
      <!-- This section is optional with the new configuration model
           introduced in .NET Framework 4. -->
      <service name="Microsoft.ServiceModel.Samples.CalculatorService"
               behaviorConfiguration="CalculatorServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
          </baseAddresses>
        </host>
        <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service  -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
        <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

什么可能使三次尝试从头开始起作用,但第四次不起作用,唯一的区别是文件的大小和添加的两个辅助类没有做任何其他事情?我的意思是,服务必须在那里,否则我会得到一个404 Not Found错误,但是元数据出了问题,尽管我在另一次尝试中使用了完全相同的
app.config
,但它仍然有效

非常感谢您的任何建议/提示。明天我肯定会尝试增加绑定中的缓冲区大小,我听说这对一些人有帮助,但我不知道我在那里,因为我还没有真正发送任何东西,是吗?我只是想添加服务引用,我不知道大小是否重要。我还尝试了TCP和basicHTTP绑定以获得相同的结果


提前感谢并为文字墙感到抱歉;我今天在工作中感到非常沮丧,由于那些愚蠢的工作条件,我甚至无法正确地研究错误,而在写这篇文章时,我又感到沮丧……;)

无法说明问题是什么,但这里有一些信息可以帮助您找到问题

当您执行“添加服务引用”时,会有一个http get调用转到该服务。我假设是这个调用返回http 400。您有一个mex端点和httpgetenabled,缺少这些是正常的问题

您可以尝试使用浏览器读取wsdl,看看是否有效

原因可能是:

  • 端口8000被阻塞
  • IIS站点位于其他端口上
  • 您正在使用计算机名访问站点,配置使用本地主机
  • iis中未为该端口启用绑定

    • 无法说明问题所在,但这里有一些信息可以帮助您找到问题

      当您执行“添加服务引用”时,会有一个http get调用转到该服务。我假设是这个调用返回http 400。您有一个mex端点和httpgetenabled,缺少这些是正常的问题

      您可以尝试使用浏览器读取wsdl,看看是否有效

      原因可能是:

      • 端口8000被阻塞
      • IIS站点位于其他端口上
      • 您正在使用计算机名访问站点,配置使用本地主机
      • iis中未为该端口启用绑定

      首先,您必须找到一个问题,我的意思是您需要知道服务器端发生了什么。您需要处理所有错误并记录它们

      记录错误

          public class GlobalErrorHanler: IErrorHandler 
          {
              //to use log4net you have to have a proper configuration in you web/app.config
              private static readonly ILog Logger = LogManager.GetLogger(typeof (GlobalErrorHandler));
      
              public bool HandleError(Exception error)
              {
                  //if you host your app on IIS you have to log using log4net for example
                  Logger.Error("Error occurred on the service side", error);
      
                  //Console.WriteLine(error.Message); 
                  //Console.WriteLine(error.StackTrace);
      
                  return false;
              }
      
              public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
              {
                  //you can provide you fault exception here
              }
          }
      
      然后是服务行为(继承属性以添加在服务实现中将其用作属性的可能性):

      然后添加到配置文件:

      <system.serviceModel>
        <extensions>
          <behaviorExtensions>
            <!-- Extension.ErrorHandlerExtention: fully qualified class name, ArrayList: assebmly name-->
            <add name="errorHandler" type="Extension.ErrorHandlerExtention, Extensions"/>
          </behaviorExtensions>
        </extensions>
      
      <behaviors>
        <serviceBehaviors>
          <behavior name="CalculatorServiceBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="False"/>
      
            <!-- the name of this element must be the same as in the section behaviorExtensions -->
            <errorHandler />
      
          </behavior>
        </serviceBehaviors>
      </behaviors>
      
      </system.serviceModel>
      
      
      ,

      跟踪:

      要启用跟踪,必须将这些行添加到配置中:

      <system.diagnostics>
        <trace autoflush="true" />
        <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true">
            <listeners>
              <add name="log"
                  type="System.Diagnostics.XmlWriterTraceListener"
                  initializeData= "C:\traces.svclog" />
            </listeners>
          </source>
        </sources>
      </system.diagnostics>
      
      
      

      首先,您必须找到一个问题,我的意思是您需要知道服务器端发生了什么。您需要处理所有错误并记录它们

      记录错误

          public class GlobalErrorHanler: IErrorHandler 
          {
              //to use log4net you have to have a proper configuration in you web/app.config
              private static readonly ILog Logger = LogManager.GetLogger(typeof (GlobalErrorHandler));
      
              public bool HandleError(Exception error)
              {
                  //if you host your app on IIS you have to log using log4net for example
                  Logger.Error("Error occurred on the service side", error);
      
                  //Console.WriteLine(error.Message); 
                  //Console.WriteLine(error.StackTrace);
      
                  return false;
              }
      
              public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
              {
                  //you can provide you fault exception here
              }
          }
      
      然后是服务行为(继承属性以添加在服务实现中将其用作属性的可能性):

      然后添加到配置文件:

      <system.serviceModel>
        <extensions>
          <behaviorExtensions>
            <!-- Extension.ErrorHandlerExtention: fully qualified class name, ArrayList: assebmly name-->
            <add name="errorHandler" type="Extension.ErrorHandlerExtention, Extensions"/>
          </behaviorExtensions>
        </extensions>
      
      <behaviors>
        <serviceBehaviors>
          <behavior name="CalculatorServiceBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="False"/>
      
            <!-- the name of this element must be the same as in the section behaviorExtensions -->
            <errorHandler />
      
          </behavior>
        </serviceBehaviors>
      </behaviors>
      
      </system.serviceModel>
      
      
      ,

      跟踪:

      要启用跟踪,必须将这些行添加到配置中:

      <system.diagnostics>
        <trace autoflush="true" />
        <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true">
            <listeners>
              <add name="log"
                  type="System.Diagnostics.XmlWriterTraceListener"
                  initializeData= "C:\traces.svclog" />
            </listeners>
          </source>
        </sources>
      </system.diagnostics>
      
      
      

      对于遇到类似问题而没有详细错误的人,我将描述我的问题是关于什么的。在我的Visual Studio项目中,我使用了一个类似于以下内容的类:

      public class Info
      {
        public string Key { get; set; }
        public string Value { get; set; }
      
        public Info(string key, string value)
        {
          this.Key = key;
          this.Value = value;
        }
      }
      
      与另一个项目(已运行)的不同之处在于,此POCO有一个接受参数的构造函数,因此不提供没有任何参数的标准构造函数。但是,这是需要的,以便对信息对象进行序列化(我希望我在这里正确使用该术语)和反序列化。因此,为了让它工作,或者只添加一个标准构造函数,它可能什么都不做,或者(也许更好)使用以下方法
      Info info = new Info { Key = "a", Value = "b" };