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

发布于 2024-10-16 16:02:03 字数 173 浏览 2 评论 0原文

我有一个由 EXE 项目和类库组成的 .NET 4 项目。该类库包含对 Web 服务的引用(使用 WCF)。 仅当我将 app.config 文件(包含有关绑定的信息)与我的 exe 一起部署时,一切才能正常工作。如何通过代码配置所有内容,而不需要部署 app.config 文件(我不希望我的用户更改这些设置)。 谢谢。 安德里亚

I have a .NET 4 project made of a EXE project and a class library. The class library contains a reference to a webservice (using WCF).
Everything works ok only if I have deployed the app.config file (that contains the info about the binding) along with my exe. How can I have everything configured by code without the need to deploy an app.config file (I don't want my users to change those settings).
Thank you.
Andrea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2024-10-23 16:02:03

您可以使用 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
{
}

那么你可以简单地使用:

using (var proxy = GetClient())
{
    // call proxy here
}

You can use the ChannelFactory class to generate proxies to your services.
Everything you configure through the configuration file can also be done using code.

You just need to instantiate an instance of the correct binding and configure its properties according to the service requirements on the other side.

For example:

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

Then you can simply use:

using (var proxy = GetClient())
{
    // call proxy here
}
淡紫姑娘! 2024-10-23 16:02:03

我就是这样做的:

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

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

This is how I did it:

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

if (embEvalServiceClient != null)
{
    embEvalServiceClient.GetPendingEvalsCompleted += getPendingEvalsCompletedHandler;
    embEvalServiceClient.GetPendingEvalsAsync(attemptId);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文