标签: webchannelfactory

在WCF中,对于webHttpBinding,当服务器使用基本身份验证时,如何在客户端web.config中指定凭据?

我有两个WCF RESTful服务 - "通用"服务是公共的,没有安全性; "admin"服务我打算使用SSL进行基本身份验证.这是我的服务器端web.config:

<system.serviceModel>
    <bindings>
        <webHttpBinding>
            <binding name="general" maxReceivedMessageSize="2147483647">
                <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
                <security mode="None">
                    <transport clientCredentialType="None" />
                </security>
            </binding>
            <binding name="admin" maxReceivedMessageSize="2147483647">
                <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
                <security mode="Transport">
                    <transport clientCredentialType="Basic" />
                </security>
            </binding>
        </webHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="web">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <services>
        <service name="MyNamespace.AppServices.GeneralService">
            <endpoint address="" binding="webHttpBinding" contract="MyNamespace.Contracts.IGeneralService" behaviorConfiguration="web" bindingConfiguration="general" />
        </service>
        <service name="MyNamespace.AppServices.AdminService">
            <endpoint address="" binding="webHttpBinding" contract="MyNamespace.Contracts.IAdminService" behaviorConfiguration="web" bindingConfiguration="admin" />
        </service>
    </services>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

在客户端,我目前的代码如下所示:

private static …
Run Code Online (Sandbox Code Playgroud)

wcf web-config credentials webhttpbinding webchannelfactory

10
推荐指数
1
解决办法
1万
查看次数

WebChannelFactory&Headers?

是否可以设置标题WebChannelFactory?如果我使用的是WebClient对象,我可以这样做:

WebClient client = new WebClient();
client.Headers.Add("referer", "http://stackoverflow.com");
client.Headers.Add("user-agent", "Mozilla/5.0");
Run Code Online (Sandbox Code Playgroud)

但是我还没有找到修改标题的方法WebChannelFactory.

c# wcf webclient webchannelfactory

7
推荐指数
1
解决办法
3024
查看次数

WCF Rest服务错误处理和WebChannelFactory

根据我的理解,以下内容应该从WCF Rest服务正确​​抛出自定义错误:

[DataContract(Namespace = "")]
public class ErrorHandler
{
    [DataMember]
    public int errorCode { get; set; }

    [DataMember]
    public string errorMessage { get; set; }
} // End of ErrorHandler

public class Implementation : ISomeInterface
{
    public string SomeMethod()
    {
        throw new WebFaultException<ErrorHandler>(new ErrorHandler()
        {
            errorCode = 5,
            errorMessage = "Something failed"
        }, System.Net.HttpStatusCode.BadRequest);
    }
}
Run Code Online (Sandbox Code Playgroud)

在Fiddler这似乎有效,我得到以下原始数据:

HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 145
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Set-Cookie: ASP.NET_SessionId=gwsy212sbjfxdfzslgyrmli1; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Thu, …
Run Code Online (Sandbox Code Playgroud)

rest wcf webchannelfactory

5
推荐指数
1
解决办法
1569
查看次数

使用WebChannelFactory创建后处置客户端

在我目前的生产代码中,根据msdn上的文档,创建客户端的方法就是这样

using (WebChannelFactory<IServiceInterface> cf
      = new WebChannelFactory<IServiceInterface>("http://service.url"))
{
    IServiceInterface client = cf.CreateChannel();
    client.CallTheMethod();
}
Run Code Online (Sandbox Code Playgroud)

鉴于我有这个界面:

public interface IServiceInterface
{
    void CallTheMethod();
}
Run Code Online (Sandbox Code Playgroud)

但是我注意到WebChannelFactory创建的对象客户端也实现了IDisposable.所以我也要处理这个对象.我没有找到任何其他方式:

using (WebChannelFactory<IServiceInterface> cf
      = new WebChannelFactory<IServiceInterface>("http://service.url"))
using(IDisposable client = (IDisposable)cf.CreateChannel())
{
    ((IServiceInterface)client).CallTheMethod();
}
Run Code Online (Sandbox Code Playgroud)

我发现这很难看.所以:

  • 我真的需要处理它吗?我的意思是,当您处理工厂时(如果工厂保留对其创建的每个对象的引用可能),它可能会被处理掉?
  • 如果是的话,你有更好的方法吗?

c# wcf dispose webchannelfactory

2
推荐指数
1
解决办法
927
查看次数