2

任何人都可以帮忙,我正在尝试通过通道工厂调用休息服务,但发送我的凭据......休息服务使用 Windows 身份验证。

但是使用以下代码,我得到“此工厂启用了手动寻址,因此发送的所有消息都必须预先寻址。” 使用 GetMessage 时出错

我知道我的服务就像我删除 Windows 身份验证一样有效!但是在打开 Windows 身份验证且不更改 clientCredentials 的情况下,我收到了 BAD REQUEST,我认为这很正常......所以我需要发送我的客户端凭据

我有点失落。

   ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000");


  var defaultCredentials = cf.Endpoint.Behaviors.Find<ClientCredentials>();
  cf.Endpoint.Behaviors.Remove(defaultCredentials); 


  // step two - instantiate your credentials
  ClientCredentials loginCredentials = new ClientCredentials();
  loginCredentials.UserName.UserName = "Test";
  loginCredentials.UserName.Password = "test";


  // step three - set that as new endpoint behavior on factory
  cf.Endpoint.Behaviors.Add(loginCredentials); //add required ones


        IService channel = cf.CreateChannel();

        Console.WriteLine(channel.GetMessage("Dhananjay Get"));

        Console.WriteLine(channel.PostMessage("Dhananjay Post"));
4

3 回答 3

3

您需要添加一个 webHttp 行为,并将该行为连接到您的端点。最终结果将如下所示:

<system.serviceModel>
  <services>
    <service ...>
       <endpoint behaviorConfiguration="webHttpBehavior" ...>
       </endpoint>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp/>
    </behavior>
    </endpointBehaviors>
  </behaviors>
  ...
 </system.serviceModel>

如果这没有帮助,请发布您的 web.config。

于 2011-01-22T07:24:54.470 回答
1

虽然这个问题有一个公认的答案,但我想我会添加一些信息。

由于类中的错误GetQueryStringConverterWebServiceFactory请参阅Microsoft Connect 报告)<WebHttp/> ,如果您通过参数传递对象数组,则无法使用。相反,将<enableWebScript/>元素添加到绑定中。

客户端 App.Config 或 Web.Config

<behaviors>
  <endpointBehaviors>
    <behavior name="WebHttp_Behaviour">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>

服务网络配置

<behaviors>
  <serviceBehaviors>
    ...
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebHttp_EndPointBehaviour">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>

至少,我不得不同时添加<enableWebScript\>到客户端和服务器。

于 2011-09-11T13:26:03.993 回答
1

对我来说,我需要添加<webHttp/>到客户端的行为。

于 2012-08-07T07:41:56.227 回答