custom code to run on server on every silverlight poll. (polling duplex) - wcf

Is it possible to run custom code when the silverlight client polls everytime to the server to keep the connection alive. My application is a implementation of the comet style polling duplex communication which is available from silverlight3.
I found that it sends the following Soap message on every poll.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<wsmc:MakeConnection xmlns:wsmc="http://docs.oasis-open.org/ws-rx/wsmc/200702">
<wsmc:Address>
http://docs.oasis-open.org/ws-rx/wsmc/200702/anoynmous?id=7f64eefe-9328-4168-8175-1d4b82bef9c3
</wsmc:Address>>
</wsmc:MakeConnection>
</s:Body>

I believe that should be possible, but having not worked with duplex services myself I can't be 100% on this. I've used the following method: System.ServiceModel.Dispatcher.IDispatchMessageInspector.AfterReceiveRequest implemented on the server side to intercept all incoming messages to strip out some security information from the message and run some custom code before it reaches the service layer.
I found this Paolo Pialorsi Article useful when implementing my solution.

Related

WCF: How to change the settings of <GenerateMessageContracts>true</GenerateMessageContracts> - PHP client

.NET Client while consuming the WCF service I am able to change GenerateMessageContracts settings manually here.
File: Reference.svcmap
<ReferenceGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="c3e3cd74-61c2-408a-a511-63a2b654abd3"
xmlns="urn:schemas-microsoft-com:xml-wcfservicemap">
<ClientOptions>
…………
<GenerateMessageContracts>true</GenerateMessageContracts>
</ClientOptions>
</ReferenceGroup>
I would like to change these setting in the WCF Service itself.
Dont know how to set GenerateMessageContracts manually for PHP client.
Anybody knows this?
You can't change that setting on the server side. The metadata exposed by the service (WSDL) which is consumed by the client side does not have any information regarding message contracts - that's a WCF term, while WSDL is a general-purpose standard. You can have two clients, one with message contracts, and one without, which send exactly the same request (and accepts the same response) to the service, and as far as the service is concerned, the requests are identical.

NServiceBus and WCF

I want to achieve the following:
Expose WCF endpoint to client from which they request a long-running operation.
Map the inbound request to a NServiceBus Message.
Publish the message to the bus for processing.
Send a reply to the client acking that their request has been received and we will begin processing it.
Bus works the message through a handler.
Can you help me with some examples here please?
Thanks in advance
You can check out the WcfIntegration sample that comes with NSB to see how to expose an endpoint via WCF. To hand off the message, you can simply call Bus.Send() to another endpoint to do processing, then use the Bus.Return() that is in the sample. From there, the other endpoint can look just like the Server part of the FullDuplex sample without the Bus.Reply() logic.
Awhile ago I created some example code that is similar to this, although it used a traditional ASMX web service and not a WCF one, but that is really just an implementation detail.
Check out NServiceBus External WebService Example on GitHub.

WCF service to queue all request

I have a wcf service and handle a lot of client (server document generation). This service should receive a lot of request and should be handle in queue. It also have a callback. (callback will return successfully generated document). I am still using PIA and will implement OpenXML in the future.
Is it wcf msmq is the way to implement this?
Is there any samples might be related? Previously its running in local machine but now want to change it as a so called "Server generated"
WCF MSMQ doesn't support callback directly - it supports only one-way operations. But for example this article discuss how to add this support. With default configuration you can send message back to original sender but it is not a callback. To support responses every client will have to expose queue and pass address of its queue as part of the request to be able to receive the message from the service. More about responses in MSMQ is in MSDN magazine.

WCF Server waiting for return before proceeding

Here is what I would like to do.
1. Service hosted in WCF
2. Client calls asking for a payload of messages
3. Service returns payload of messages and waits for client to respond
3.A. Client returns 200 (OK) status or something confirming messages received.
3.B. Client returns bad error status stating to not delete the messages on server.
4. Depending on 3.A or 3.B Service will take appropriate action.
I would like to do this by doing something like extending IDispatcher and writing extension methods. VS creating another service and having the client call that service to signal which messages it received. Unless that's best practices.
Thanks in advanced.
If acting on HTTP status codes is a requirement then WCF is probably not what you want to use. WCF was created to be able to write transport independent code so the bindings could be changed purely through configuration; no code changes required. The HTTP request handling is buried so deeply into HTTP-based bindings that you're better off using something like the OpenRasta framework to implement your HTTP (REST) style service. It is a very HTTP request aware framework.
Otherwise, look at this wsDualHttpBinding intro to accomplish something similar through the application API level.

Can I listen to a WCF event from a web client?

Can I listen to a WCF event from a web client? Is this possible? I am not talking about call backs, I want the WCF service to raise and event and the web client to be able to listen. Is there a good example of this in C#?
There are no events in WCF. If you want mimic event you still have to call some operation exposed on all clients = you must call WCF service or callback exposed on client.
What do you mean by web client? Do you mean javascript code running in web browser? In such case no you can't achieve that with WCF. You can only use AJAX calls from borowser and continuously poll the service for possible "event".
If you mean ASP.NET application then the answer is theoretically yes, practiacally it will be pretty hard. The reason is that in ASP.NET you handle only current HTTP request by some handler - for example Page. The lifetime of the handler is only for serving the single request. Due to that using duplex service doesn't make to much sense because for receiving callbacks by duplex service your client proxy must live. If you open the proxy in Page it will die after serving the request. If you open the proxy in separate thread you must somehow corelate incomming callbacks to actual client but the client still have to poll the web server to be notified about callbacks. Similar situation will be with exposing the service on ASP.NET application.
Difference between asynchronnous and duplex calls is big. In asynchronnous pattern single request always have single response. Resonse is not sent without request. In duplex pattern you can make single request and receive thousands callback from server.