How can I change EndPoint address in WCF? - wcf

I have a client app and a server app.
The client calls a wcf service and passes machine information
to server , based on the machine name the server calls back a wcf service on client side.
So to achieve this , I am just changing the EndPointAddress but then it's throwing
NoEndPointFoundException , how can i fix it , below is the code :
public void RegisterTasks(MachineConfig machineInfo)
{
string add = exeProxy.Endpoint.Address.Uri.Scheme + "://" + machineInfo.MachineName.Trim()+"/"
+ exeProxy.Endpoint.Address.Uri.Segments[1];
Uri uri = new Uri(add);
EndpointAddress eadd = new EndpointAddress(add);
WSHttpBinding whttpBinding = new WSHttpBinding(SecurityMode.None);
//ServiceReference1.ExecuteTaskClient newProxy = new ExecuteTaskClient(whttpBinding , eadd);
//EndpointAddress endPointAddress = ;
exeProxy.Endpoint.Address = eadd;
//exeProxy.Endpoint.Binding = new System.ServiceModel.BasicHttpBinding("httpBinding");
// we just execute the task by
// calling the wcf service on client side
foreach (Task task in machineInfo.Tasks)
{
exeProxy.ExecuteTask(task.TaskID);
// newProxy.ExecuteTask(task.TaskID);
}
}

I am assuming that you are getting EndpointNotFoundException not NoEndPointFoundException. I could not find a reference to NoEndPointFoundException.
http://msdn.microsoft.com/en-us/library/system.servicemodel.endpointnotfoundexception.aspx
What this is saying is that the client cannot find the server. In your case the server is trying to call back to the client, so the roles are reversed.
There are two things that could be wrong:
the url that you have in the variable "add" is incorrect (try logging the value)
the wcf service on the client side is not listening on the correct url (try putting the url in a browser)
Hope this helps
Shiraz

My first impression is that your design should be reconsidered. Any time I see an ingenious (read: bizarre) solution I see a whole heap of head banging in the making.
So firstly, write out what it is that you are trying to acheive in baby speak: "I want client to be contacted when server has new data." and then think about how it can be acheived using more conventional techniques.
Check out the duplex bindings - and polling a stateful singleton is not always a bad idea if you're not scaling into thousands of clients -- in fact, I bet it'd scale better than your current design.
But, to solve your current design issue, I'd setup the client (which will become the server) with the MEX endpoint (mexHttpBinding) and then set it off so its listening, then use VS (an empty project or from the server project) to try and connect to the client(server) by way of Add Service Reference and supplying the local machine name etc. This alone might turn up your problem. Then once added, you can use the autogen app.config to know what settings you need.
Also, have you considered how the server will be able to call the client if the server doesn't have the proxy classes setup?
It does sound like you're making a rod for your own back.

Related

How to implement dynamic endpoint in WCF

i am new in wcf. Dynamic Endpoint is a standard endpoint which performs discovery and automatically selects a matching service that i know. here is a code sample for DynamicEndpoint.
DynamicEndpoint dynamicEndpoint = new DynamicEndpoint(ContractDescription.GetContract(typeof(ICalculatorService)), new WSHttpBinding());
CalculatorServiceClient client = new CalculatorServiceClient(dynamicEndpoint);
Console.WriteLine("Invoking CalculatorService");
Console.WriteLine();
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
i do not understand from the code that it is assumed that we added service reference and then we work with dynamic endpoint. when we add wcf service reference at client side then endpoint related setting automatically added in config file at client end.
could anyone please tell how dynamic endpoint discover my service address at run time just if we pass the contract. suppose if we have no config file at client end then how dynamic endpoint can discover my service?
could anyone please tell me in what kind of situation dynamic endpoint is used and only option ?
please discuss all my points in details. thanks
There are multiple ways of discovering services. There is UDDI and WS-Discovery.
It seems those classes use WS-Discovery. For a better overview, I'd suggest a good book, this is way to broad for a single SO question.

Config Framework and WIF Federation+Delegation: Need factory.CreateChannelActingAs(token)

My current task is to secure a WCF service. The service is hosted using the configuration framework (5.5, released with the StockTraider sample) and the caller uses the configuration framework as well.
I managed to secure the connection using ws2007FederationHttpBinding.
For the "IsOnline()"-Check my STS issues a service token and this works already but for the actual service calls, I want to have ActAs-Tokens to still know the real user inside the called service.
My STS is capable of issuing the correct ActAs-Tokens.
The problem is the loadbalancing client, which always opens the factory and I cannot call the WIF-methods (ConfigureChannelFactory() and CreateChannelActingAs()) anymore, because they require the factory to be in the created state.
My best try is this, but it looses the ActAs-Subject somewhere and feels like a hack:
IPSServiceClient = new Client(serviceName, settingsInstance, createNewChannelInstance: true);
var token = ((IClaimsIdentity)Thread.CurrentPrincipal.Identity).BootstrapToken;
var factoryObject = IPSServiceClient.createANewChannelFactoryByAddress(IPSServiceClient.getANodeAddress());
var factory = factoryObject as ChannelFactory<IIWBPortalServiceV1>;
factory.ConfigureChannelFactory(); //factory must not be state=open here
factory.Credentials.SupportInteractive = false; //no cardspace
_channel = factory.CreateChannelActingAs(token);
Do I miss an extensibility point in the config framework? What is the best way I should go?
If I make a new console app, add service reference and add the two calls (ConfigureChannelFactory() and CreateChannelActingAs()) it just works!
The posted code inside my questions works. The problem was the web.config of the STS which was missing AudienceUris inside the ActAs-securityTokenHandlers section.
Still: The posted code feels like a hack to me.

How can I check connection is still valid for Silverlight client using net.tcp for duplex?

I'm putting together a WCF service using net.tcp and netTcpBinding to get duplex comms with my Silverlight client. I call into the service from the Silverlight app and the service calls out to another server, passing it a callback method in the WCF service class. The remote server calls back several times and each time it does, the WCF service uses the callbackchannel to send the data to the Silverlight client. It all works nicely most of the time.
If the user puts in a big request, I get a TimeoutException after a large number of callbacks have already worked. (Clearly, there's some work to do elsewhere to prevent this but I'd like to robustify the service, first.)
I was expecting to do some kind of 'if (client.ConnectionState == faulted)' check before trying to call back to the Silverlight client but I can't seem to find the object that holds the state of the connection. Is there one? Am I approaching this from the wrong side?
This is my first venture into a service net.tcp and duplex. I just moved house and my WCF bible is still in a box. Somewhere. :-) So, I can't do my usual background reading.
Any pointers would be gratefully received. Here's some bare code in case my description is too soupy:
private IActiveDirectoryClient client;
private AsyncSearchRunner runner;
public void Search(Request request)
{
this.client = OperationContext.Current.GetCallbackChannel<IActiveDirectoryClient>();
runner = new AsyncSearchRunner();
runner.Run(request.SearchRoot, request.SearchFilter, request.PageSize,
System.DirectoryServices.Protocols.SearchScope.Subtree, SendObjects);
}
private void SendObjects(List<DirectoryObject> items)
{
Response response = new Response();
response.DirectoryObjects = items.ToArray();
client.SendResponse(response);
}
Yes, there is a State property that is defined in the ClientBase<> class (all the proxy classes are derived from ClientBase<>).
There are some proxy wrappers out there that handle fault states of the connection and re-establish connections as needed. Google for "wcf proxy wrapper".
You can also home-brew something if you use some kind of ServiceLocator pattern.

Detect when client connected to wcf service

From a little bit of reading around, it is my understanding that the only way to detect that a client has connected to my service is through writing my own code. I am using a Singleton service. I would like to display a message every time a client connects to my service that client x with ip xxx has connected. There is no built-in event that is generated? Am I correct?
No, I don't think there's any support in WCF for your requirement.
Not sure what you want to achieve with this, either. Your service class (in your case, just a single instance) really doesn't have any business putting up messages (on screen, I presume) - that really not it's job. The service class is used to handle a request and deliver a response - nothing more.
The ServiceHost class might be more of a candidate for this feature - but again, it's job really is to host the service, spin up the WCF runtime etc. - and it's really not a UI component, either.
What you could possibly do is this
have an Admin UI (a Winforms, console, or WPF app) running on your server alongside your service, providing an admin service to call
define a fast connection between the two services (using e.g. netNamedPipe binding which is perfect for intra-application messaging)
when your "real" service gets a call, the first thing it does is send out a message to the admin UI which can then pick up that message and handle it
That way, you could cleanly separate your real service and it's job (to provide that service) and the Admin UI stuff you want to do and build a cleanly separated system.
I have actually implemented my own connect, disconnect and ping service methods which I manually call from my client once the channel has been created. By using them as a kind of header section in all of my ServiceContract interface definitions (and their implementations, of course), they form an makeshift "base service definition" that only requires a bit of cut-n-paste.
The string-based parameters of connect and disconnect will be used to send client info to the server and return server info and (perhaps a unique connection id) to the client. In addition a set of timing reference points may make its way in also.
Note how SessionMode is required and the individual OperationContract properties IsInitiating and IsTerminating are explicitly specified for each method, the end result being what I would call a "single-session" service in that it defines connect and disconnect as the sole session bookends.
Note also that the ping command will be used as the target of a timer-based "heartbeat" call that tests the service connection state and defeats ALL connection timeouts without a single config file :-)
Note also that I haven't determined my fault-handling structure yet which may very well add a method or more and/or require other kinds of changes.
[ServiceContract( SessionMode = SessionMode.Required )]
public interface IRePropDalSvr {
[OperationContract( IsInitiating=true, IsTerminating=false )]
string connect (string pClientInfo);
[OperationContract( IsInitiating=false, IsTerminating=true, IsOneWay=true )]
void disconnect (string pClientInfo);
// ------------------------------------------------------------------------------------------
[OperationContract( IsInitiating=false, IsTerminating=false )]
string ping (string pInp);
// ------------------------------------------------------------------------------------------
// REST OF ServiceContract DEFINITION GOES HERE
One caveat: while I am currently using this code and its implemention in my service classes, I have not verified the code yet.

wcf - transfer context into the headers

I am using wcf 4 and trying to transparently transfer context information between client and server.
I was looking at behaviors and was able to pass things around. My problem is how to flow the context received in the incoming headers to the other services that might be called by a service.
In the service behavior I intercept the the message and read the headers but don't know where to put that data to be accessible to the next service call that the current service might make.
What I am looking for is something like:
public void DoWork()
{
var someId = MyContext.SomeId;
//do something with it here and call another service
using(var proxy = GetProxy<IAnotherService>())
proxy.CallSomeOtherMethodThatShouldGetAccessTo_ MyContextualObject();
}
If I store the headers in thread local storage I might have problems due to thread agility(not sure this happens outside ASP.NET, aka custom service hosts). How would you implement the MyContext in the code above.
I chose the MyContext instead of accessing the headers directly because the initiator of the service call might not be a service in which case the MyContext is backed by HttpContext for example for storage.
In the service behavior I intercept
the the message and read the headers
but don't know where to put that data
to be accessible to the next service
call.
Typically, you don't have any state between calls. Each call is totally autonomous, each call gets a brand new instance of your service class created from scratch. That's the recommended best practice.
If you need to pass that piece of information (language, settings, whatever) to a second, third, fourth call, do so by passing it in their headers, too. Do not start to put state into the WCF server side! WCF services should always be totally autonomous and not retain any state, if at ever possible.
UPDATE: ok, after your comments: what might be of interest to you is the new RoutingService base class that will be shipped with WCF 4. It allows scenarios like you describe - getting a message from the outside and forwarding it to another service somewhere in the background. Google for "WCF4 RoutingService" - you should find a number of articles. I couldn't find antyhing in specific about headers, but I guess those would be transparently transported along.
There's also a two-part article series Building a WCF Router Part 1 (and part 2 here) in MSDN Magazine that accomplishes more or less the same in WCF 3.5 - again, not sure about headers, but maybe that could give you an idea.