Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
没有配置文件的WCF配置_Wcf_Configuration_Configuration Files - Fatal编程技术网

没有配置文件的WCF配置

没有配置文件的WCF配置,wcf,configuration,configuration-files,Wcf,Configuration,Configuration Files,有谁知道一个很好的例子,说明如何在不使用配置文件的情况下以编程方式公开WCF服务?我知道现在使用WCF的服务对象模型更加丰富,所以我知道这是可能的。我只是没有看到一个这样做的例子。相反,我想看看在没有配置文件的情况下如何进行消费 在任何人询问之前,我有一个非常具体的需求,需要在没有配置文件的情况下执行此操作。我通常不推荐这样的做法,但正如我所说,在这种情况下有一个非常特殊的需求。在服务器上不容易 对于客户端,您可以使用所有WCF配置都可以通过编程完成。因此,可以在不使用配置文件的情况下创建服务器

有谁知道一个很好的例子,说明如何在不使用配置文件的情况下以编程方式公开WCF服务?我知道现在使用WCF的服务对象模型更加丰富,所以我知道这是可能的。我只是没有看到一个这样做的例子。相反,我想看看在没有配置文件的情况下如何进行消费


在任何人询问之前,我有一个非常具体的需求,需要在没有配置文件的情况下执行此操作。我通常不推荐这样的做法,但正如我所说,在这种情况下有一个非常特殊的需求。

在服务器上不容易


对于客户端,您可以使用

所有WCF配置都可以通过编程完成。因此,可以在不使用配置文件的情况下创建服务器和客户端


我推荐Juval Lowy的《编程WCF服务》一书,其中包含许多编程配置示例。

如我所发现的,在没有配置文件的情况下使用web服务非常简单。您只需创建绑定对象和地址对象,并将它们传递给客户端代理的构造函数或通用ChannelFactory实例。您可以查看default 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" ) );
}

我发现下面关于这个话题的链接上的博文非常有趣

我喜欢的一个想法是,能够将绑定、行为或地址XML部分从配置传递到适当的WCF对象,并让它处理属性的分配——目前您无法做到这一点

与web上的其他人一样,我在需要WCF实现使用不同于宿主应用程序(这是一个.NET 2.0 Windows服务)的配置文件时遇到了一些问题


在客户端和服务器端都很容易做到。朱瓦尔·洛伊的书中有很好的例子

至于你对配置文件的评论,我想说的是,配置文件是一个穷人的第二次做它的代码。当您控制将连接到服务器的每个客户端并确保它们已更新,并且用户无法找到它们并更改任何内容时,配置文件非常有用。我发现WCF配置文件模型有局限性,设计起来有点困难,而且是维护的噩梦。总而言之,我认为MS将配置文件作为默认的处理方式是一个非常糟糕的决定


编辑:配置文件不能做的一件事是使用非默认构造函数创建服务。这会导致WCF中出现静态/全局变量、单例和其他类型的无意义。

如果您有兴趣取消web.config中的System.ServiceModel部分用于IIS托管,我在这里发布了一个如何执行此操作的示例()。我将演示如何自定义ServiceHost以创建元数据和wshttpbinding端点。我是以一种通用的方式来做的,不需要额外的编码。对于那些没有立即升级到.NET 4.0的用户来说,这可能非常方便。

这里,这是完整的工作代码。我想这对你会有很大帮助。我一直在搜索,从来没有找到一个完整的代码,这就是为什么我试图把完整的工作代码。祝你好运

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;
    }
}

我个人喜欢这种方法,例如,当您打算在不同的事务中使用该文件时,例如,如果您已经加密了app.config(或等效的配置文件),并且不需要使用内置的WCF功能在连接中读取https,请添加binding.Security.Mode=BasicHttpSecurityMode.Transport;这对我来说很有效。对我来说唯一的区别是,我还设置了readerquota和安全信息。我利用了ciscoheat的建议,如果使用https,则将Security.Transport.Mode设置为Transport(对于我来说,这在编译时是未知的)。我只是验证了所有被设置的属性都等于WCF 4、fwiw中的默认值。(但请注意,
Security.Mode
默认为
None
)John,我相信这是一篇很棒的博客文章,但既然17个月前有一个公认的答案,你的答案真的有什么意义吗?因为这是我的第一个堆栈溢出答案,可能不是通常的做法。由于熟悉Lowy和Bustamante的书,这些书都是很好的参考资料,我认为我的答案远远超出了它们提供的样本。我主要在谷歌搜索时使用堆栈溢出,所以我经常阅读较旧的帖子。从我的角度来看,有更多最新的答案只会有所帮助。在编写代码之前,我在谷歌上搜索了这篇文章,以避免重新发明轮子。作为SO的常客,我发现阅读关于旧主题的新文章是非常可取的。它帮助我更好地完成我的工作,这增加了这个网站的价值(因为我和其他人会更多地访问它)。与其严格遵守规则,为什么不允许人们讨论,以便找到更好的答案?这不是重点吗?约翰·桑德斯似乎是在回答他自己的问题时被放在了自己的位置上的(我想补充一下,他没有接受任何一个问题的答案)。我个人对迟交的问题没有异议,而且通常很高兴看到我问的问题在几个月甚至几年后有了新的回答。具有讽刺意味的是,我用自己对这个问题的公认答案赢得了自己的亡灵巫师徽章我也有同样的问题,被接受的答案对我没有帮助,但这确实帮助了我,为迟到的答案欢呼!如果不是因为迟来的回答,我就不得不重复这个问题。非常好的例子!您可以演示手动配置的几乎每个方面。干得好!我不明白EvalServiceClient如何适应这段代码。它被引用,但未定义。为什么服务器要创建客户机?为什么不推荐这样的做法(以编程方式公开服务而不进行配置)?