C#WCF ChannelFactory BadRequest 400错误

时间:2013-08-26 09:08:23

标签: c# wcf proxy webchannelfactory

我创建了一个简单的Web服务服务器,但是为了从同一项目中的服务类调用方法,我创建了一个webchannelfactory代理。一切正常,直到我将项目(或exe版本)移动到另一台机器。我无法弄明白发生了什么!

我收到此错误消息:

The remote server returned an unexpected response: (400) Bad Request.

使用Stacktrace:

Server stack trace:
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResp
onse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factor
y, WebException responseException, ChannelBinding channelBinding)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpCha
nnelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeS
pan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message messag
e, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean on
eway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan tim
eout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCall
Message methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage req
Msg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa
ta, Int32 type)
   at Client.IClientService.dosomething()
   at Host.ClientHost.Start(Uri hostUri, Int32 interval) in C:\(...)

代码:

WebChannelFactory<Client.IClientService> factory = new WebChannelFactory<Client.IClientService>(hostUri);

Client.IClientService proxy = factory.CreateChannel();

proxy.dosomething();  // Error shout here on Machine2

提前致谢

EDIT1: 对于此示例,没有客户端(我已将此客户端命名为Client)。正如我所说,它正在使用的代码在另一台机器上没有错误(都使用localhost)。

<?xml version="1.0"?>
<configuration>
      <appSettings>
      <system.serviceModel>
        <bindings>
        </bindings>
    <services>
      <service name="Client.ClientService" behaviorConfiguration="Default">
        <endpoint address="" binding="webHttpBinding" contract="Client.IClientService"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true"></serviceMetadata>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

EDIT2:

在浏览器上,我得到了这个:

  

服务器在处理请求时遇到错误。例外   message是'Client.ClientService'的类型初始值设定项   例外。'。请参阅服务器日志以获取更多详异常堆栈   追踪是:

     

在CreateClient.ClientService()at的Client.ClientService..ctor()处   System.ServiceModel.Dispatcher.InstanceProvider.GetInstance(的InstanceContext   instanceContext,消息消息)at   System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance(的InstanceContext   instanceContext,消息请求)at   System.ServiceModel.InstanceContext.GetServiceInstance(消息   消息)   System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&安培;   rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean   isOperationContextSet)

1 个答案:

答案 0 :(得分:0)

解决了问题。

该服务使用数据库,因此当我将项目复制到新机器时,我忘了创建一个新数据库。