Wcf 如何在不使用app.config的情况下在.NET 4中配置Web服务

Wcf 如何在不使用app.config的情况下在.NET 4中配置Web服务,wcf,deployment,app-config,Wcf,Deployment,App Config,我有一个由EXE项目和类库组成的.NET4项目。类库包含对使用WCF的Web服务的引用。 只有在我部署了app.config文件(该文件包含绑定信息以及我的exe)的情况下,一切正常。如何通过代码配置所有内容而无需部署app.config文件我不希望我的用户更改这些设置。 非常感谢。 安德里亚我就是这样做的: MyServiceResponseClient embEvalServiceClient = new MyServiceResponseClient (new BasicHttpBindi

我有一个由EXE项目和类库组成的.NET4项目。类库包含对使用WCF的Web服务的引用。 只有在我部署了app.config文件(该文件包含绑定信息以及我的exe)的情况下,一切正常。如何通过代码配置所有内容而无需部署app.config文件我不希望我的用户更改这些设置。 非常感谢。 安德里亚我就是这样做的:

MyServiceResponseClient embEvalServiceClient = new MyServiceResponseClient (new BasicHttpBinding(),
                                                    new EndpointAddress(new Uri(url)));

if (embEvalServiceClient != null)
{
    embEvalServiceClient.GetPendingEvalsCompleted += getPendingEvalsCompletedHandler;
    embEvalServiceClient.GetPendingEvalsAsync(attemptId);
}
我就是这样做的:

MyServiceResponseClient embEvalServiceClient = new MyServiceResponseClient (new BasicHttpBinding(),
                                                    new EndpointAddress(new Uri(url)));

if (embEvalServiceClient != null)
{
    embEvalServiceClient.GetPendingEvalsCompleted += getPendingEvalsCompletedHandler;
    embEvalServiceClient.GetPendingEvalsAsync(attemptId);
}

您可以使用ChannelFactory类生成服务的代理。 通过配置文件配置的所有内容也可以使用代码完成

您只需要实例化正确绑定的实例,并根据另一端的服务需求配置其属性

例如:

private IDisposableService GetClient()
{
    var netBinding = new BasicHttpBinding();
    netBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    netBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

    var factory = new ChannelFactory<IDisposableService>(netBinding, new EndpointAddress(new Uri(ServerUrl)));
    factory.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
    factory.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

    var channel = factory.CreateChannel();

    return channel;
}

interface IDisposableService : IYourService, IDisposable
{
}

您可以使用ChannelFactory类生成服务的代理。 通过配置文件配置的所有内容也可以使用代码完成

您只需要实例化正确绑定的实例,并根据另一端的服务需求配置其属性

例如:

private IDisposableService GetClient()
{
    var netBinding = new BasicHttpBinding();
    netBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    netBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

    var factory = new ChannelFactory<IDisposableService>(netBinding, new EndpointAddress(new Uri(ServerUrl)));
    factory.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
    factory.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

    var channel = factory.CreateChannel();

    return channel;
}

interface IDisposableService : IYourService, IDisposable
{
}