当前位置:  开发笔记 > 编程语言 > 正文

没有配置文件的WCF配置

如何解决《没有配置文件的WCF配置》经验,为你挑选了4个好方法。

有没有人知道如何在不使用配置文件的情况下以编程方式公开WCF服务的一个很好的例子?我知道服务对象模型现在使用WCF更丰富,所以我知道它是可能的.我只是没有看到如何这样做的例子.相反,我希望看到没有配置文件的消费也是如此.

在有人要求之前,我有一个非常具体的需要,没有配置文件.我通常不会推荐这样的做法,但正如我所说,在这种情况下有一个非常具体的需要.



1> devios1..:

正如我所发现的那样,使用没有配置文件的Web服务非常简单.您只需创建一个绑定对象和地址对象,并将它们传递给客户端代理的构造函数或通用的ChannelFactory实例.您可以查看默认的app.config以查看要使用的设置,然后在实例化代理的地方创建一个静态帮助器方法:

internal static MyServiceSoapClient CreateWebServiceInstance() {
    BasicHttpBinding binding = new BasicHttpBinding();
    // I think most (or all) of these are defaults--I just copied them from app.config:
    binding.SendTimeout = TimeSpan.FromMinutes( 1 );
    binding.OpenTimeout = TimeSpan.FromMinutes( 1 );
    binding.CloseTimeout = TimeSpan.FromMinutes( 1 );
    binding.ReceiveTimeout = TimeSpan.FromMinutes( 10 );
    binding.AllowCookies = false;
    binding.BypassProxyOnLocal = false;
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    binding.MessageEncoding = WSMessageEncoding.Text;
    binding.TextEncoding = System.Text.Encoding.UTF8;
    binding.TransferMode = TransferMode.Buffered;
    binding.UseDefaultWebProxy = true;
    return new MyServiceSoapClient( binding, new EndpointAddress( "http://www.mysite.com/MyService.asmx" ) );
}


对于https用法,添加binding.Security.Mode = BasicHttpSecurityMode.Transport;
我刚刚验证了所有设置的属性都等于WCF 4中的默认值fwiw.(但请注意`Security.Mode`默认为'None`.)

2> John Wigger..:

如果您有兴趣消除IIS主机的web.config中System.ServiceModel部分的使用,我已经发布了一个如何在此处执行此操作的示例(http://bejabbers2.blogspot.com/2010/02/wcf -zero-config-in-net-35-part-ii.html).我将展示如何自定义ServiceHost以创建元数据和wshttpbinding端点.我是以通用的方式进行的,不需要额外的编码.对于那些没有立即升级到.NET 4.0的人来说,这可能非常方便.


作为一个频繁的SO用户,我发现阅读旧主题的新帖子是非常可取的.它帮助我更好地完成我的工作,这增加了这个网站的价值(因为我和其他人会更多地访问它).为什么不让人们讨论如此更好的答案,而不是成为规则的坚持者?这不是重点吗?
因为这是我的第一个Stack Overflow答案,可能不是通常的事情.熟悉Lowy和Bustamante书籍,这些都是很好的参考,我认为我的答案远远超出了他们提供的样本.我主要在谷歌搜索时使用Stack Overflow,所以我经常阅读较旧的帖子.从我的角度来看,拥有更多最新答案只会有所帮助.我在编写代码之前用Google搜索了这篇文章,以避免重新发明轮子.
似乎约翰·桑德斯回答了他自己的问题(他没有接受我可能添加的答案).我个人对迟到的问题回复没有任何问题,我很高兴看到对我提出的问题的新回复,几个月甚至几年之后.具有讽刺意味的是,我在这个问题的答案中获得了自己的死灵法师徽章.:)
我有同样的问题,接受的答案对我没有帮助,但这样做了,欢呼迟到的答案!如果不是迟到的答案,我将不得不创建一个重复的问题.

3> 小智..:

在这里,这是完整且有效的代码.我认为它会对你有很大帮助.我正在搜索,从来没有找到完整的代码,这就是为什么我试图提供完整和有效的代码.祝好运.

public class ValidatorClass
{
    WSHttpBinding BindingConfig;
    EndpointIdentity DNSIdentity;
    Uri URI;
    ContractDescription ConfDescription;

    public ValidatorClass()
    {  
        // In constructor initializing configuration elements by code
        BindingConfig = ValidatorClass.ConfigBinding();
        DNSIdentity = ValidatorClass.ConfigEndPoint();
        URI = ValidatorClass.ConfigURI();
        ConfDescription = ValidatorClass.ConfigContractDescription();
    }


    public void MainOperation()
    {
         var Address = new EndpointAddress(URI, DNSIdentity);
         var Client = new EvalServiceClient(BindingConfig, Address);
         Client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust;
         Client.Endpoint.Contract = ConfDescription;
         Client.ClientCredentials.UserName.UserName = "companyUserName";
         Client.ClientCredentials.UserName.Password = "companyPassword";
         Client.Open();

         string CatchData = Client.CallServiceMethod();

         Client.Close();
    }



    public static WSHttpBinding ConfigBinding()
    {
        // ----- Programmatic definition of the SomeService Binding -----
        var wsHttpBinding = new WSHttpBinding();

        wsHttpBinding.Name = "BindingName";
        wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
        wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.BypassProxyOnLocal = false;
        wsHttpBinding.TransactionFlow = false;
        wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        wsHttpBinding.MaxBufferPoolSize = 524288;
        wsHttpBinding.MaxReceivedMessageSize = 65536;
        wsHttpBinding.MessageEncoding = WSMessageEncoding.Text;
        wsHttpBinding.TextEncoding = Encoding.UTF8;
        wsHttpBinding.UseDefaultWebProxy = true;
        wsHttpBinding.AllowCookies = false;

        wsHttpBinding.ReaderQuotas.MaxDepth = 32;
        wsHttpBinding.ReaderQuotas.MaxArrayLength = 16384;
        wsHttpBinding.ReaderQuotas.MaxStringContentLength = 8192;
        wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 4096;
        wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 16384;

        wsHttpBinding.ReliableSession.Ordered = true;
        wsHttpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
        wsHttpBinding.ReliableSession.Enabled = false;

        wsHttpBinding.Security.Mode = SecurityMode.Message;
        wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
        wsHttpBinding.Security.Transport.Realm = "";

        wsHttpBinding.Security.Message.NegotiateServiceCredential = true;
        wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
        wsHttpBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256;
        // ----------- End Programmatic definition of the SomeServiceServiceBinding --------------

        return wsHttpBinding;

    }

    public static Uri ConfigURI()
    {
        // ----- Programmatic definition of the Service URI configuration -----
        Uri URI = new Uri("http://localhost:8732/Design_Time_Addresses/TestWcfServiceLibrary/EvalService/");

        return URI;
    }

    public static EndpointIdentity ConfigEndPoint()
    {
        // ----- Programmatic definition of the Service EndPointIdentitiy configuration -----
        EndpointIdentity DNSIdentity = EndpointIdentity.CreateDnsIdentity("tempCert");

        return DNSIdentity;
    }


    public static ContractDescription ConfigContractDescription()
    {
        // ----- Programmatic definition of the Service ContractDescription Binding -----
        ContractDescription Contract = ContractDescription.GetContract(typeof(IEvalService), typeof(EvalServiceClient));

        return Contract;
    }
}


我不明白EvalServiceClient如何适合这段代码.它被引用,但没有定义.为什么服务器创建客户端?

4> Gulzar Nazim..:

在服务器端并不容易..

对于客户端,您可以使用ChannelFactory

推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有