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

wes*_*ang 14 wcf config configuration-files

我刚刚在一个月前开始研究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>
Run Code Online (Sandbox Code Playgroud)

你能帮我改变一下吗?

Sam*_*der 25

您可以在代码中进行所有设置,假设您将来不需要灵活地更改此设置.

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

基本上你想做的事情如下:

// 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);
Run Code Online (Sandbox Code Playgroud)

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