试图添加在一个项目中错误请求(400)的服务引用的结果,否则运行良好 [英] Trying to add a service reference results in Bad Request (400) in one project, otherwise runs fine
问题描述
我在一个微妙的局面:正如标题所暗示的,我似乎无法连接到WCF服务我在Windows服务包裹起来。我跟着教程 http://msdn.microsoft .COM / EN-US /库/ ms733069%28V = vs.110%29.aspx 的每一步,得到它的工作的多次的这种具体的方式,只是不适合的有一个的具体项目。我真的不知道它是什么,我有一个非常简单的接口只有一个方法的合同,我的服务已安装就好了,也开始就好了。有一次,我尝试在另一个项目中添加一个服务引用我得到一个错误400,错误请求和元数据的问题。
我甚至改写了工程样机(它基本上与合同的履行少code中的同一个项目,但我们谈论仍低于300线code)的跑完全正常,并得出了同样的错误。我没有改变任何的的app.config
code,当我这样做,我可以前连接,但错误的请求之后。
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.
什么增加的问题是,我不能发表任何code在这里(我工作的一个虚拟机在那里我没有上网的工作,再加上互联网其实我对物理机是如此的限制,我不能打开任何类型板/论坛/博客/什么的,所以我不能确切的发布错误)。因为我所有的例子极少做的工作,我的最小不工作的例子将是总code无妨。
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.
任何建议/提示是大大AP preciated。我一定会尝试在明天的结合增加缓冲区大小,听说帮助一些人,但我不知道我的存在,因为我真的不发送任何东西,我是谁?我只是想添加服务参考,我不知道,如果大小事务那里。我也已经尝试过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
}
}
然后服务行为(继承属性添加可能使用它作为服务实现的属性):
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.
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:\traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
的跟踪工具的:跟踪查看
这篇关于试图添加在一个项目中错误请求(400)的服务引用的结果,否则运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!