c# - Create ServiceEndpoint in code and in config
This is actually quite this is expecially useful when you are working with some hosted environment, where you have no control of deploying the app.config, but constructing the binding element in code gives you some advantage.
supppose that you have something in the app.config in one of the client application, and it is like this:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<client>
<endpoint name="TabularPushService"
address="net.tcp://127.0.0.1:9999/TabularPushService"
binding="netTcpBinding"
contract="WcfPub.ITabularPushService">
</endpoint>
</client>
</system.serviceModel>
</configuration>
With this in in app.config, you can code your program with something like this:
static void Main(string[] args)
{
InstanceContext instanceContext = new InstanceContext(new TabularPushCallback());
ServiceEndpoint endpoint = new ServiceEndpoint(
ContractDescription.GetContract(typeof(ITabularPushService)),
new NetTcpBinding(),
new EndpointAddress("net.tcp://127.0.0.1:9999/TabularPushService"));
using (DuplexChannelFactory<ITabularPushService> channelFactory = new DuplexChannelFactory<ITabularPushService>(instanceContext, "TabularPushService"))
{
ITabularPushService proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
proxy.Subscribe(0);
proxy.Subscribe(1);
Console.WriteLine("the table {0} {1} registered", 0, proxy.IsSubscribed(0) ? "is" : "is not");
Console.WriteLine("the table {0} {1} registered", 9, proxy.IsSubscribed(9) ? "is" : "is not");
proxy.UnSubscribe(0);
Console.Read();
}
}
}
However, you can change the code to something like this:
static void Main(string[] args)
{
InstanceContext instanceContext = new InstanceContext(new TabularPushCallback());
ServiceEndpoint endpoint = new ServiceEndpoint(
ContractDescription.GetContract(typeof(ITabularPushService)),
new NetTcpBinding(),
new EndpointAddress("net.tcp://127.0.0.1:9999/TabularPushService"));
using (DuplexChannelFactory<ITabularPushService> channelFactory = new DuplexChannelFactory<ITabularPushService>(instanceContext, endpoint))
{
ITabularPushService proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
// same as before
Console.Read();
}
}
}
Remember the rule that anything in bindingElemenmt within the config can be rewritten in code.
Another note to add, sometimes, you don't need to explicitly create a ServiceEndpoint instance and pass that to caller which needs it. like in the case of a ServiceHost.AddServiceEndpoint methods, an example is shown as below.
using (ServiceHost host = new ServiceHost(typeof(TabularPushService)))
{
host.AddServiceEndpoint(
typeof(ITabularPushService),
new NetTcpBinding(),
string.Format("net.tcp://127.0.0.1:{0}/TabularPushService", arguments.Port));
host.Open();
Application app = new Application();
app.Run(window);
}
as you can see, you can pass a raw string as the endpoint value.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.