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
Related
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!
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())));
I'd like to configure a NetTcpBinding programatically for a silverlight 4 client. (NetTcpBinding is now supported)
Here is the code I use to do this for a Windows Forms client:
EndpointAddress endpointAddress = new EndpointAddress(uri);
NetTcpBinding netTcpBinding = new NetTcpBinding();
MyServiceClient agentClient = new MyServiceClient(new InstanceContext(this), netTcpBinding, endpointAddress);
For silverlight I added references to System.ServiceModel.Extensions and System.ServiceModel.NetTcp, but this is not not enough : I'm not able to find a NetTcpBinding class.
Where is this class if it exists? Does an equivalent syntax exists? The silverlight 4 runtime must be doing this somehow when a configuration file is used.
You can use a custom binding in place of NetTcpBinding : the code below is working, but I don't know if this is the recommended pattern.
BinaryMessageEncodingBindingElement messageEncoding = new BinaryMessageEncodingBindingElement();
TcpTransportBindingElement tcpTransport = new TcpTransportBindingElement();
CustomBinding binding = new CustomBinding(messageEncoding, tcpTransport);
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);