尝试在一个项目中添加服务引用导致错误请求 (400),否则运行良好 [英] Trying to add a service reference results in Bad Request (400) in one project, otherwise runs fine

查看:13
本文介绍了尝试在一个项目中添加服务引用导致错误请求 (400),否则运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于一个微妙的情况:正如标题所暗示的,我似乎无法连接到我在 Windows 服务中包装的 WCF 服务.我按照教程 http://msdn.microsoft.com/en-us/library/ms733069%28v=vs.110%29.aspx每一步并让它以这种方式多次工作,只是不适合一个特定项目.我真的不知道它是什么,我有一个非常简单的界面,只有一种方法作为合同,我的服务安装得很好,启动也很好.一旦我尝试在另一个项目中添加服务引用,我就会收到错误 400、错误请求和元数据问题.我什至重写了原型项目(它基本上是同一个项目,在合同的实施中代码更少;但我们仍然在 300 行代码以下),它运行得非常好,并出现了同样的错误.当我这样做时,我没有更改任何 app.config 代码,我之前可以连接,但之后请求错误.

I'm in a delicate situation: As the title suggests, I can't seem to connect to a WCF service I wrapped up in a Windows Service. I followed the tutorial http://msdn.microsoft.com/en-us/library/ms733069%28v=vs.110%29.aspx every step and got it to work multiple times in this exact way, just not for one particular project. I really don't know what it is, I have a very simple interface with just one method as contract, my service is installed just fine and also starts just fine. Once I try to add a service reference in another project I get an error 400, Bad Request, and a metadata problem. I even rewrote the prototype project (it's basically the same project with less code in the implementation of the contract; but we're talking still below 300 lines of code) which ran perfectly fine, and came to the same error. I didn't change any of the app.config code when I did that, and I could connect before, but bad request afterwards.

增加问题的是我不能在这里发布任何代码(我正在工作的虚拟机上工作,我没有互联网访问权限,加上我在物理机上实际拥有的互联网访问权限是如此受限,我无法打开任何类型的板/论坛/博客/任何内容,因此我无法发布确切的错误).因为我所有的最小示例都可以工作,所以我的最小不工作示例"将是总代码.

What adds to the problem is that I can't post any code here (I'm working on a VM at work where I don't have internet access, plus the internet access I actually do have on the physical machine is so restricted I can't open any kind of board/forum/blog/whatever, so I can't post exact errors). Since all my minimal examples do work, my "minimal not working example" would be the total code anyway.

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

I'm at a total loss here. I dug through all the other topics of the bad request error and have some more checking to do tomorrow, but I thought I'd rather just post here and maybe get some advice on what/how to further test my project for errors. Just in case it helps, the app.config is the same as the following except for the service and contract names:

<?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>

什么可能从头开始进行三次尝试,但第四次不能再次工作,唯一的区别是文件的大小和 2 个添加的辅助类,它们没有做任何其他事情?我的意思是,该服务必须在那里,否则我会得到 404 Not Found 错误,但元数据有问题,尽管我使用的是完全相同的(!)app.config再试一次就成功了.

What could possibly make three attempts from scratch work, but the 4th not work again where the only difference is the size of the files and 2 added helper classes which are not doing anything else? I mean, the service has to be there, otherwise I'd get a 404 Not Found error instead, but something's wrong with the metadata although I'm using the exact(!) same app.config in another try and there it works.

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

Any suggestions/hints are greatly appreciated. I'll surely try increasing the buffer sizes in the binding tomorrow, I heard that helped some people, but I don't know about me there since I'm not really sending anything yet, am I? I'm just trying to add the service reference, I don't know if the size matters there. I also already tried TCP and basicHTTP bindings to the same result.

预先感谢并为文字墙感到抱歉;我今天在工作中感到非常沮丧,由于那些愚蠢的工作条件,我什至无法正确研究错误,而这种沮丧再次出现在写这篇文章......;)

Thanks in advance and sorry for the wall of text; I got very frustrated at work today, with not even being able to research the error properly due to those stupid work conditions, and that frustration came up again writing this... ;)

推荐答案

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

For the beginning you have to find an issue, I mean you need to know what happens on server side. You need to handler all error and log them.

记录错误

    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
        }
    }

然后是服务行为(继承 Attribute 以增加将其用作服务实现上的属性的可能性):

Then service behavior (inherits Attribute to add possibility to use it as an attribute on service implementation):

public class MyErrorHandlingBehavior : Attribute, IServiceBehavior
{

    public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
        return;
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher disp in serviceHostBase.ChannelDispatchers)
            disp.ErrorHandlers.Add(new GlobalErrorHanler());
    }

    public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {        }
}

和扩展元素以在您的配置中使用它:

And extension element to use it in your config:

public class ErrorHandlerExtention: BehaviorExtensionElement
{

    public override Type BehaviorType
    {
        get { return typeof(MyErrorHandlingBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new MyErrorHandlingBehavior();
    }
}

然后添加到配置文件:

<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>

这会让你得到一个错误.当您遇到错误时,请返回论坛.

This will allow you to get an error. When you have an error get back to the forum.

更多使用这种技术的帖子:错误 1, 错误 2

Some more posts with this technique: Error 1, Error 2

跟踪:

要打开跟踪,您必须将这些行添加到配置中:

To turn on tracing you have to add such lines to a config:

<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:	races.svclog" />
      </listeners>
    </source>
  </sources>
</system.diagnostics>

跟踪工具:跟踪查看器

这篇关于尝试在一个项目中添加服务引用导致错误请求 (400),否则运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆