wcf错误的请求错误 [英] wcf bad request error

查看:68
本文介绍了wcf错误的请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在WCF世界中还很陌生,并且已经创建了一项服务(非常简单)

这是合同

[ServiceContract]
公共接口IBrokerTransferService
{

[OperationContract]
void PutMessageIntoTable(BrokerMessage消息);
}

这是合同的执行情况
公共类BrokerTransferService:IBrokerTransferService
{

公共无效PutMessageIntoTable(BrokerMessage消息)

{
//将消息添加到表中

}

这是我的数据合同类

[DataContract]
   公共类BrokerMessage
    {
       [DataMember]
     公共字符串TransactionID;
       [DataMember]
     公共字符串MessageID;
       [DataMember]
     公共DateTime时间戳;
       [DataMember]
     公共SenderInfo SenderInfo;
       [DataMember]
     公共字符串PacketID;
      [DataMember]
     公共字符串PacketCount;
       [DataMember]
     公共字符串MessagePayload;
    }
这是配置文件

< system.serviceModel>
    <服务>
     <服务名称="Mary.BrokerTransferService.BrokerTransferService">
       < host>
         < baseAddresses>
           < add baseAddress =" http://localhost/BrokerTransferService/" />
         </baseAddresses>
       </host>
       <!-服务端点->
       <!-除非完全合格,否则地址是相对于上面提供的基地址->
       <端点地址=基本".
] binding ="basicHttpBinding"
]合约="Mary.BrokerTransferService.IBrokerTransferService">
       </endpoint>
      
       <端点地址="mex"; binding ="mexHttpBinding" contract ="IMetadataExchange"/>
     </service>
    </services>
    <行为>
     < serviceBehaviors>
       <行为>
         <!-为避免泄露元数据信息,
        将下面的值设置为false并在部署之前删除上面的元数据终结点->
         < serviceMetadata httpGetEnabled ="True"/>
         <!-要接收故障中的异常详细信息以进行调试,
        将下面的值设置为true.部署前设置为false
        避免泄露异常信息->
         < serviceDebug includeExceptionDetailInFaults =" False" />
       </行为>
     </serviceBehaviors>
    </行为>
  </system.serviceModel>

然后创建一个控制台应用程序来托管此服务.

类程序
    {
      静态void Main(string [] args)
       {
           ServiceHost主机=新的ServiceHost(typeof(BrokerTransferService));
          试试
           {
                             host.Open();
                             Console.WriteLine(启动并运行");
                             PrintServiceInfo(host);
                             Console.Read();
                             host.Close();
           }
           catch(Exception ex)
           {
                             Console.WriteLine(ex);
                             Console.Read();
                             host.Abort();
           }
       }
      静态void PrintServiceInfo(ServiceHost主机)
       {
           foreach(host.Description.Endpoints中的var ep)
           {
                             Console.WriteLine(ep.Address);
           }
       }
    }

如果我运行Host应用,则表明该服务已启动并正在运行,并显示配置文件中的所有端点. >

这是主机控制台应用程序的配置文件

< system.serviceModel>
    <行为>
     < serviceBehaviors>
       <行为名称="ServiceBehavior">
         < serviceMetadata/>
       </行为>
     </serviceBehaviors>
    </行为>
    <服务>
     <服务名称="Mary.BrokerTransferService.BrokerTransferService" behaviorConfiguration =" ServiceBehavior">
       < endpoint address =" http://localhost/BrokerTransferService/basic "
         binding ="basicHttpBinding"合同="Mary.BrokerTransferService.IBrokerTransferService"</endpoint>
       < endpoint address =" http://localhost:8081/BrokerTransferService/MEX " binding ="mexHttpBinding" contract ="IMetadataExchange"</endpoint>
     </service>
    </services>
  </system.serviceModel>

到此为止,一切看起来都正常.

但是,如果我想创建一个客户端应用程序来使用我的服务(在运行Host应用程序之后),请向该应用程序添加引用

我总是

"
没有端点在监听 http://localhost:8081/BrokerTransferService/MEX 可以接受该消息.这通常是由不正确的地址引起的 或SOAP操作.有关更多详细信息,请参见InnerException(如果存在).
无法连接到远程服务器
尝试对无法访问的网络[:: 1]:8081进行套接字操作
如果服务是在当前解决方案中定义的,请尝试构建解决方案并再次添加服务引用.
"

如果尝试从浏览器中请求它,则会收到此消息(Http 400错误请求)

有人可以帮助我找出此处的错误之处

提前感谢

PS:我已经读了很多书,试图找出原因以及类似问题的人,但是没有一种解决方案适合我.

彼得

解决方案

是否有任何理由不将MEX终结点托管在与其余服务相同的域中?如果没有,请尝试将主机应用程序及其配置更改为以下内容,它应该可以正常工作:

主机应用:

类程序{静态void Main(string [] args){ServiceHost主机=新的ServiceHost(typeof(BrokerTransferService));尝试{host.Open(); Console.WriteLine(启动并运行"); PrintServiceInfo(host); Console.Read(); host.Close(); } catch(Exception ex){Console.WriteLine(ex); Console.Read(); host.Abort(); }} static void PrintServiceInfo(ServiceHost host){foreach(host.Description.Endpoints中的var ep){Console.WriteLine(ep.Address); }}} 

配置:

< system.serviceModel> <行为> < serviceBehaviors> <行为名称="ServiceBehavior"> < serviceMetadata/> < serviceMetadata httpGetEnabled ="true"/> </行为> </serviceBehaviors> </行为> <服务> <服务名称="Mary.BrokerTransferService.BrokerTransferService" behaviorConfiguration ="ServiceBehavior" <主机> < baseAddresses> < add baseAddress =" http://localhost/BrokerTransferService/" /> </baseAddresses> </host> <端点地址=基本" binding ="basicHttpBinding"合同="Mary.BrokerTransferService.IBrokerTransferService"/<端点地址="mex"; binding ="mexHttpBinding" contract ="IMetadataExchange"/</service> </services></system.serviceModel> 



Hi everyone,

i am quite new in WCF world and I have create a service (very simple one)

here is the contract

[ServiceContract]
public interface IBrokerTransferService
{

[OperationContract]
void PutMessageIntoTable(BrokerMessage message);
}

This is the implementation of the contract
public class BrokerTransferService: IBrokerTransferService
{

public void PutMessageIntoTable(BrokerMessage message)

{
// Add Message to the table

}

This is my Data contract class

[DataContract]
    public class BrokerMessage
    {
        [DataMember]
       public string TransactionID;
        [DataMember]
       public string MessageID;
        [DataMember]
       public DateTime Timestamp;
        [DataMember]
       public SenderInfo SenderInfo;
        [DataMember]
       public string PacketID;
       [DataMember]
       public string PacketCount;
        [DataMember]
       public string MessagePayload;
    }
Here is the config file

<system.serviceModel>
    <services>
      <service name="Mary.BrokerTransferService.BrokerTransferService">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost/BrokerTransferService/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="basic"
                  binding="basicHttpBinding"
                  contract="Mary.BrokerTransferService.IBrokerTransferService">           
        </endpoint>
       
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information,
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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>
  </system.serviceModel>

then I create a console app to Host this service.

class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(BrokerTransferService));
            try
            {
                host.Open();
                Console.WriteLine("Up and running");
                PrintServiceInfo(host);
                Console.Read();
                host.Close();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
                Console.Read();
                host.Abort();
            }
        }
        static void PrintServiceInfo(ServiceHost host)
        {
            foreach (var ep in host.Description.Endpoints)
            {
                Console.WriteLine(ep.Address);
            }
        }
    }

If I run the Host app, it shows that the service is Up and running and display all the endpoints from the config file. 

Here is the config file for the Host console App

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Mary.BrokerTransferService.BrokerTransferService" behaviorConfiguration = "ServiceBehavior">
        <endpoint address="http://localhost/BrokerTransferService/basic"
          binding="basicHttpBinding" contract="Mary.BrokerTransferService.IBrokerTransferService"></endpoint>
        <endpoint address="http://localhost:8081/BrokerTransferService/MEX" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      </service>
    </services>
  </system.serviceModel>

Until this point everything looks that is working fine.

But if I want to create a client application to consume my service (after run the Host app) adding reference to it

I always got

"There was an error downloading 'http://localhost:8081/BrokerTransferService/MEX'.
Unable to connect to the remote server
A socket operation was attempted to an unreachable network [::1]:8081
Metadata contains a reference that cannot be resolved: 'http://localhost:8081/BrokerTransferService/MEX'.
There was no endpoint listening at http://localhost:8081/BrokerTransferService/MEX that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server
A socket operation was attempted to an unreachable network [::1]:8081
If the service is defined in the current solution, try building the solution and adding the service reference again.
"

If I try to request it from the browser I got this (Http 400 bad request )

Could someone help me to figure out what is wrong here,

Thanks in advance

PS: I have read a lot trying to find why and people with similar issue, but non of the solutions works for me.

Peter

解决方案

Is there any reason why you don't host the MEX endpoint in the same domain as the rest of the service? If not, try changing the host app and its config to the following, and it should work:

Host app:

class Program{    static void Main(string[] args)    {        ServiceHost host = new ServiceHost(typeof(BrokerTransferService));        try        {            host.Open();            Console.WriteLine("Up and running");            PrintServiceInfo(host);            Console.Read();            host.Close();        }        catch(Exception ex)        {            Console.WriteLine(ex);            Console.Read();            host.Abort();        }    }    static void PrintServiceInfo(ServiceHost host)    {        foreach (var ep in host.Description.Endpoints)        {            Console.WriteLine(ep.Address);        }    }}

Config:

<system.serviceModel>  <behaviors>    <serviceBehaviors>      <behavior name="ServiceBehavior">        <serviceMetadata />        <serviceMetadata httpGetEnabled="true"/>      </behavior>    </serviceBehaviors>  </behaviors>  <services>    <service name="Mary.BrokerTransferService.BrokerTransferService" behaviorConfiguration = "ServiceBehavior">      <host>        <baseAddresses>          <add baseAddress = "http://localhost/BrokerTransferService/" />        </baseAddresses>      </host>      <endpoint address="basic"        binding="basicHttpBinding" contract="Mary.BrokerTransferService.IBrokerTransferService"/>      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>    </service>  </services></system.serviceModel>



这篇关于wcf错误的请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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