如何在没有配置文件中设置的情况下创建WCF客户端



我一个月前刚开始研究WCF。如果我问了一些已经回答过的问题,请原谅我。我试着先搜索,但一无所获。

我读过这篇文章,WCF文件传输:流媒体&IIS中托管的阻塞通道。它非常有效。现在,我喜欢将客户端代码集成到我的应用程序中,该应用程序是在AutoCAD中运行的dll。如果我想使用配置文件,我必须更改acad.exe.config,我认为这不是一个好主意。所以我想如果可能的话,我想把配置文件中的所有代码都移到代码中。

这是配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Mtom" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://10.1.13.15:88/WCFStreamUpload/service.svc/ep1"
            binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService"
            contract="MGFileServerClient.IService"
            name="BasicHttpBinding_IService" />
    </client>
</system.serviceModel>

你能帮我换一下吗?

您可以从代码中进行所有设置,假设您不需要在将来更改这一点的灵活性。

您可以在MSDN上阅读有关设置端点的信息。虽然这适用于服务器,但端点和bindingd的配置也适用于客户端,只是您使用不同的类。

基本上你想做一些类似的事情:

// Specify a base address for the service
EndpointAddress endpointAdress = new EndpointAddress("http://10.1.13.15:88/WCFStreamUpload/service.svc/ep1");
// Create the binding to be used by the service - you will probably want to configure this a bit more
BasicHttpBinding binding1 = new BasicHttpBinding();
///create the client proxy using the specific endpoint and binding you have created
YourServiceClient proxy = new YourServiceClient(binding1, endpointAddress);

显然,您可能希望使用与上面的配置相同的安全性、超时等配置绑定(您可以在MSDN上阅读BasicHttpBinding),但这应该会让您朝着正确的方向前进。

这完全是基于代码的配置和工作代码。您不需要任何客户端配置文件。但至少你需要一个配置文件(可能是自动生成的,你不必考虑)。所有配置设置都在代码中完成。

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

您是否希望保留自定义配置并在应用程序中引用它?你可以试试这篇文章:从自定义位置读取WCF配置(关于WCF)

否则,您可以使用ConfigurationManager.OpenExeConfiguration.

相关内容

  • 没有找到相关文章

最新更新