Benutzer:MovGP0/WCF/Endpoints
< Benutzer:MovGP0 | WCF
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Endpoints[Bearbeiten | Quelltext bearbeiten]Consists of:
Konfiguration[Bearbeiten | Quelltext bearbeiten]
namespace MyNamespace
{
[ServiceContract]
interface IMyServiceContract
{
// ...
}
class MyService : IMyServiceContract, ISomeOtherContract
{
// ...
}
}
<system.serviceModel>
<services>
<service name="MyNamespace.MyService">
<!-- using base address; only possible when transport schemes are the same -->
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/" />
</baseAddresses>
</host>
<!-- protocol mapping for base address (optional; not possible for TCP, IPC, MSMQ; not possible imperatively) -->
<protocolMapping>
<add scheme="http" binding="wsHttpBinding" />
<add scheme="https" binding="wsHttpBinding" bindingConfiguration="NamedConfiguration" />
</protocolMapping>
<endpoint binding="wsHttpBinding" contract="MyNamespace.IMyContract" />
<!-- using specific address -->
<endpoint address="http://localhost:8080/MyService" binding="wsHttpBinding" contract="MyNamespace.IMyContract" />
<!-- additional endpoints for same contract using different addresses -->
<endpoint address="http://localhost:80/MyService" binding="wsHttpBinding" contract="MyNamespace.IMyContract" />
<endpoint address="http://localhost:80/MyService2" binding="wsHttpBinding" contract="MyNamespace.IMyContract" />
<!-- additional endpoints for same contract using different bindings -->
<endpoint address="net.tcp://localhost:808/MyService" binding="netTcpBinding" contract="MyNamespace.IMyContract" />
<!-- additional endpoints for different contracts -->
<endpoint address="http://localhost:8080/OtherService" binding="wsHttpBinding" contract="MyNamespace.ISomeOtherContract" />
</service>
</services>
</system.serviceModel>
Declarative Binding Configuration[Bearbeiten | Quelltext bearbeiten]<system.serviceModel>
<services>
<service name="MyNamespace.MyService">
<!-- uses a named binding -->
<endpoint address="http://localhost:8080/OtherService"
binding="wsHttpBinding" bindingConfiguration="TransactionalTCP"
contract="MyNamespace.ISomeOtherContract" />
</service>
</services>
<bindings>
<!-- configuration options depend on binding type; look up documentation -->
<netTcpBinding name="TransactionalTCP" transactionFlow="true" />
<!-- default bindings are unnamed -->
<!-- effects all endpoint which don't use a named binding -->
<!-- at most one default binding per binding type -->
<netTcpBinding transactionFlow="true" />
</bindings>
</system.serviceModel>
Imperative Binding Configuration[Bearbeiten | Quelltext bearbeiten]use using(var host = new ServiceHost(typeof(MyService))
{
// named binding
Binding wsBinding = new WSHttpBinding("OptionalBindingName");
// binding with configuration
Binding tcpBinding = new NetTcpBinding
{
TransactionFlow = true
};
host.AddServiceEndpoint(typeof(IMyContract), wsBinding, "http://localhost:8000/MyService");
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "net.tcp://localhost:8001/MyService");
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "net.tcp://localhost:8002/MyService");
host.Open();
// ...
}
var baseAddress = new Uri("net.tcp://localhost:8000");
using(var host = new ServiceHost(typeof(MyService), baseAddress)
{
Binding tcpBinding = new NetTcpBinding();
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, ""); // net.tcp://localhost:8000
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "MyService"); // net.tcp://localhost:8000/MyService
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "net.tcp://localhost:8001/MyService");
host.Open();
// ...
}
Configure()[Bearbeiten | Quelltext bearbeiten]
class MyService : IMyContract
{
// must be public static
public static void Configure(ServiceConfiguration config)
{
Binding wsBinding = new WSHttpBinding();
Binding tcpBinding = new NetTcpBinding();
config.EnableProtocol(new BasicHttpBinding()); // (optional) set default endpoint
config.AddServiceEndpoint(typeof(IMyContract), wsBinding, "http://localhost:8000/MyService");
config.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "net.tcp://localhost:8001/MyService");
config.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "net.tcp://localhost:8002/MyService");
// Hint: use LoadFromConfiguration() to load section from web.config
}
}
|