I have a WCF service self-hosted in my console application and trying to understand the hostNameComparisonMode binding attribute for basicHttpBinding. Here is my hosted WCF:
// Test for StrongWildCard
ServiceHost serviceHostForStrong = new ServiceHost(typeof(Service), new Uri("http://test1:8887/Service"));
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
EndpointAddress endpointAddress = new EndpointAddress("http://abc1:8887/Service/ForStrong");
ServiceEndpoint serviceEndpoint =
new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceContract)),
basicHttpBinding,
endpointAddress);
serviceHostForStrong.AddServiceEndpoint(serviceEndpoint);
serviceHostForStrong.Open();
// Test for Exact
ServiceHost serviceHostForExact = new ServiceHost(typeof(Service), new Uri("http://test2:8888/Service"));
basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.Exact;
endpointAddress = new EndpointAddress("http://abc2:8888/Service/ForExact");
serviceEndpoint =
new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceContract)),
basicHttpBinding,
endpointAddress);
serviceHostForExact.AddServiceEndpoint(serviceEndpoint);
serviceHostForExact.Open();
// Test for Weak
ServiceHost serviceHostForWeak = new ServiceHost(typeof(Service), new Uri("http://test3:8889/Service"));
basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.WeakWildcard;
endpointAddress = new EndpointAddress("http://abc3:8889/Service/ForWeak");
serviceEndpoint =
new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceContract)),
basicHttpBinding,
endpointAddress);
serviceHostForWeak.AddServiceEndpoint(serviceEndpoint);
serviceHostForWeak.Open();
Console.WriteLine("Service is running....Press any key to exit");
And here is my client side code:
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8887/Service/ForStrong/ABC"); // Why it doesn't match with StrongWildCard
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
ChannelFactory<IServiceContract> channelFactory = new ChannelFactory<IServiceContract>(basicHttpBinding, endpointAddress);
IServiceContract proxy = channelFactory.CreateChannel(endpointAddress);
Console.WriteLine("Data received: " + proxy.GetData());
Console.ReadKey();
As far as I understand from http://kennyw.com/?p=109, the URL "http://localhost:8887/Service/ForStrong/ABC" should match the StrongWildCard. However, it gives an exception at client when I try to run this code:
An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll Additional information: The message with To 'http://localhost:8887/Service/ForStrong/ABC' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.
Am I missing anything here or my understand is incorrect? Few more supporting questions:
Can you give me few examples (considering this as base) where it
will use WeakWildCard URL?
How would I know which server side URL
served my request?
Thanks!
Related
I am trying to understand "receiveTimeout" binding property in WCF by following code:
Server side:
ServiceHost serviceHostForStrong = new ServiceHost(typeof(Service), new Uri("http://localhost:8887/Service"));
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8887/Service/");
ServiceEndpoint serviceEndpoint =
new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceContract)),
basicHttpBinding,
endpointAddress);
serviceHostForStrong.AddServiceEndpoint(serviceEndpoint);
serviceHostForStrong.Open();
Console.WriteLine("Service is running....Press any key to exit");
Console.ReadKey();
Client side:
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8887/Service/");
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
ChannelFactory<IServiceContract> channelFactory = new ChannelFactory<IServiceContract>(basicHttpBinding, endpointAddress);
IServiceContract proxy = channelFactory.CreateChannel(endpointAddress);
Console.WriteLine("Data received: " + proxy.GetData());
Thread.Sleep(new TimeSpan(0, 0, 10));
Console.WriteLine("Data received after 10 seconds: " + proxy.GetData());
Console.WriteLine("Press any key to exit.....");
Console.ReadKey();
Please note:
The receiveTimeout property set on server side is 5 seconds.
I'm waiting for 20 seconds in GetData() method on server side before returning the data.
I'm waiting for 10 seconds on client side before sending another request.
The application works fine without any exception. Ideally, in my opinion, it should throw an exception (as per my understanding from the definition of MSDN for receiveTimeout).
Thoughts anyone?
Thanks!
ReceiveTimeout – used by the Service Framework Layer to initialize the session-idle timeout which controls how long a session can be idle before timing out.
So, to get that behavior you have to use wsHttpBinding binding (it supports session) instead of basicHttpBinding (no session, hence receive no timeout).
Is there any way that we can use netnamedpipe binding with duplex ?
I am getting the following error.
Contract requires Duplex, but Binding 'NetNamedPipeBinding' doesn't support it or isn't configured properly to support it.
ServiceHost host = new ServiceHost(typeof(MyService));
NetNamedPipeBinding npb = new NetNamedPipeBinding();
npb.MaxBufferSize = Int32.MaxValue;
npb.MaxReceivedMessageSize = Int32.MaxValue;
npb.OpenTimeout = new TimeSpan(200000);
npb.CloseTimeout = new TimeSpan(200000);
npb.SendTimeout = new TimeSpan(200000);
npb.TransferMode = TransferMode.Streamed;
host.AddServiceEndpoint(typeof(IMyService), npb, "net.pipe://localhost/MyService");
host.Open(); // I am getting above error here
Please guide me.
Duplex communication works with a net named pipe binding. Try removing:
npb.TransferMode = TransferMode.Streamed;
I'm using Castle WCF Integration Facility and I have everything working properly for my first webHttp endpoint. For this endpoint to work, it requires that the endpoint have the WebHttpBehavior enabled. I was able to achieve this using:
container.Register(Component.For<IEndpointBehavior>()
.ImplementedBy<WebHttpBehavior>());
This becomes a problem when I try to enable a second endpoint using BasicHttpBinding which is not compatible with the WebHttpBehavior.
Is there someway to specify that the IEndPointBehavior registration above is only applicable to a certain endpoint?
This is my full installer for the service:
container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
.Register(Component.For<IDiagnosticService>()
.ImplementedBy<DiagnosticService>()
.Named("DiagnosticService")
.LifestyleTransient()
.AsWcfService(new DefaultServiceModel()
.Hosted()
.AddEndpoints(WcfEndpoint.BoundTo(new WebHttpBinding()).At("json"))
.AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding()).At("soap"))
.PublishMetadata(o => o.EnableHttpGet())));
container.Register(Component.For<IEndpointBehavior>()
.ImplementedBy<WebHttpBehavior>());
Ok. I finally figured this out. Turns out that the majority of my problem had to do with the Azure emulation environment rather than Castle WCF Integration. The answer is pretty straight forward -- just setup the ServiceEndpoint instances and use the WcfEndpoint.FromEndpoint() method.
Here is my working installer:
String internalEndpointAddress = string.Format("http://{0}/DiagnosticService.svc",
RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint);
// This ContractDescription instance must be used for both endpoints in this case
ContractDescription description = ContractDescription.GetContract(typeof(IDiagnosticService));
// Create JSON webHTTP Binding
WebHttpBinding webhttpbinding = new WebHttpBinding();
string jsonURI = internalEndpointAddress + "/json";
EndpointAddress jsonEndpointAddress = new EndpointAddress(new Uri(jsonURI));
ServiceEndpoint jsonEndpoint = new ServiceEndpoint(description, webhttpbinding, jsonEndpointAddress);
jsonEndpoint.Behaviors.Add(new WebHttpBehavior());
// Create WSHTTP Binding
WSHttpBinding wsHttpBinding = new WSHttpBinding();
string soapURI = internalEndpointAddress + "/soap";
EndpointAddress soapEndpointAddress = new EndpointAddress(new Uri(soapURI));
ServiceEndpoint soapEndpoint = new ServiceEndpoint(description, wsHttpBinding, soapEndpointAddress);
container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
.Register(Component.For<IDiagnosticService>()
.ImplementedBy<DiagnosticService>()
.Named("DiagnosticService")
.LifestyleTransient()
.AsWcfService(new DefaultServiceModel()
.Hosted()
.AddEndpoints(WcfEndpoint.FromEndpoint(jsonEndpoint))
.AddEndpoints(WcfEndpoint.FromEndpoint(soapEndpoint))
.PublishMetadata(o => o.EnableHttpGet())));
Given the Following Code how Would i Change/Set my Silverlight WCF Service URI in code?
mySvc.InsertPOCompleted += new EventHandler<SalesSimplicityPO_SL.POSvc.InsertPOCompletedEventArgs>(mySvc_InsertPOCompleted);
mySvc.InsertPOAsync(InitialsTextBox.Text.ToString(), DescTextBox.Text.ToString(), ClientTextBox.Text.ToString());
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress(new Uri("http://localhost/POSystem/POSvc.svc"));
POSvc.POSvcClient mySvc = new POSvc.POSvcClient(binding, address);
How can I add proxy server to my custom binding in WCF?
I found the solution:
HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();
httpsTransport.ProxyAddress = new Uri("proxyaddress");
httpsTransport.MaxReceivedMessageSize = 2147483647; stsBinding.Elements.Add
(httpsTransport);
where stsBinding is our type of CustomBinding