Silverlight 4 NetTcpBinding programmatic configuration - wcf

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);

Related

Create WCF Client dynamically

I want to create a dynamic Connector, to connect to my WCF Service.
I found the following code in the internet:
BasicHttpBinding basic = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
basic.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
EndpointAddress serviceAddress = new EndpointAddress("http://url.svc");
ServiceClient client = new ServiceClient(basic, serviceAddress);
The problem is that in this case I need to know what 'ServiceClient' is. Is it possible to create the Client without the Type?

Configuring a WCF and StructureMap ServiceHostFactory from code

I am creating a custom ServiceHost object and configuring it from code. My service is using InstanceContextMode.Single and ConcurrencyMode.Multiple and is hosted in a windows service.
As stated in a number of blogs/articles (here), sharing a StructureMap container across instances requires using a custom InstanceProvider, ServiceBehavior and ServiceHostFactory.
My initialization code looks like this. I do not use a config file.
var baseAddress = ConfigurationManager.AppSettings["BaseAddress"];
var port = Int32.Parse(ConfigurationManager.AppSettings["Port"]);
Host = new MyServiceHost(typeof(MediaFileServicePrivate), new Uri(string.Format(baseAddress, port)));
var binding = new NetTcpBinding();
Host.AddServiceEndpoint(typeof(IMediaFileServicePrivate), binding, string.Format(baseAddress, port));
How do I tell the service to use my custom service host factory? All the examples I can find configure it from the config file.
Is a ServiceHostFactory only used for IIS/WAS hosted scenarios? If so, how do I use SM for a self-hosted InstanceContextMode.Single service?
Has this not been answered? essentially you tell it to use your servicefactory in the wcf markup
<%# ServiceHost Language="C#" Debug="true" Service="WcfWithDI.Service1" CodeBehind="Service1.svc.cs" Factory="WcfWithDI.MyServiceFactory"%>
I have a sample project here all wired up with a custom provider (and structuremap)
https://github.com/billCreativeD/WCF_With_DI
Unfortunately it makes little sense to say "the service uses the factory". You would use the Ninject factory to create your service:
var factory = new NinjectServiceHostFactory();
var address = new Uri(_baseAddress, path);
ServiceHostBase host = factory.CreateServiceHost(typeName, new[] {address});
var binding = new NetTcpBinding();
host.AddServiceEndpoint(typeof(TheType), binding, string.Format(baseAddress, port));
The directive in the .svc file is used to tell .NET how to instantiate your service.

Specifying Castle WCF Integration Facility Endpoint Behavior per Endpoint

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?

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);

Reading from ServiceReferences.ClientConfig in WCF in Silverlight 3 in a dynamically loaded .xap file

I'm using Silverlight 3 Prism (CAB) with WCF
When I call a WCF service in a Prism module, I get the same error:
"Could not find default endpoint element that references contract 'IMyService' in the service model client configuaration section. This might be because no configuaration file was found for your application or because no end point element matching this contract could be found in the client element"
It turns out that its looking in the Shell's .xap file for a ServiceReferences.ClientConfig file, not in the module's ServiceReferences.ClientConfig file. I added my endpoint and binding to the existing ServiceReferences.ClientConfig file in my Silverlight Shell application (it calls it's own WCF services).
Then I had to rebuild the Shell app to generate the new .xap file for my Web project's ClientBin folder.
Next I changed to setting up the service in code:
// create the binding elements
BinaryMessageEncodingBindingElement binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement()
{ MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue};
HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement()
{ MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };
// add the binding elements into a Custom Binding
CustomBinding customBinding;
if (Application.Current.Host.Source.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase))
{
customBinding = new CustomBinding(binaryMessageEncoding, httpsTransport);
}
else
{
customBinding = new CustomBinding(binaryMessageEncoding, httpTransport);
}
// create the Endpoint URL
EndpointAddress endpointAddress = new EndpointAddress(
"http://localhost/Test/TestModule/Test.TestModule.WCF/TestModuleService.svc");
// create an interface for the WCF service
var service = new TestModuleServiceClient(customBinding, endpointAddress);
This post deals with a similar situation:
http://blogs.southworks.net/matiasb/2009/06/20/how-to-consume-wcf-services-from-composite-application-guidance-for-wpf-and-silverlightprism-v2-modules/
Thanks,
Damian Schenkelman
http://blogs.southworks.net/dschenkelman