Service reference proxy

Topics: Web Client Software Factory, Project Management Forum, User Forum
Mar 25, 2008 at 1:00 PM
Edited Mar 25, 2008 at 2:41 PM
Hi
Until now I have copied over an autogenerated proxy from my Web Service Software Factory solution to my WCSF solution. Now I'm trying to "Add Service Reference" in my WCSF solution but then I have to create Request/Response objects like this:

GetCustomerRequest request = new GetCustomerRequest(ID);
GetCustomerResponse response = serviceClient.GetCustomer(request);
return response.GetCustomerResponseResult;

I would rather do it this way (with the parameters directly in the request call):

return serviceClient.GetCustomer(ID);

Simple and plain. Can this be done and why is this request/response objects the default? And at last; where is the proxy-code that is generated by VS2008 when using "Add Service Reference"?
Mar 26, 2008 at 8:59 AM
Edited Apr 2, 2008 at 10:25 AM
I found that if you're using the proxy client class genereated by VS2008 you get what I wanted. If you use the service class or interface as a proxy you get the other (ref. my first bolded code in first post). I feel that I dont really understand this. Can someone make this clear for me?
What is best practice? I guess the client class generated by VS2008 is the prefered way to do it? Because I get this code example when I have successfully created a Web Service (when I run the WCF-service in debug mode in Visual Studio):
class Test
{
static void Main()
{
MyServiceClient client = new MyServiceClient();

// Use the variable "client" to call service operations.

// Always close the client.*
client.Close();
}
}

Should I also register this client when registrering the service as a global service in my ServiceProxy module?

protected virtual void AddGlobalServices(IServiceCollection globalServices)
{
globalServices.AddNew(typeof(MyServiceClient), typeof(MyServiceClient));
}

I'm a bit confused when it comes to this and my next problem will be how do I create my own proxy wrapper so I better can handle timeouts and unhandled exceptions? I'll maybe make a new post for that ;-)
Mar 26, 2008 at 10:49 AM
Edited Mar 26, 2008 at 10:49 AM
Some comments below:
Mar 26, 2008 at 4:12 PM
Edited Apr 2, 2008 at 1:04 PM

Should I also register this client when registrering the service as a global service in my ServiceProxy module?

protected virtual void AddGlobalServices(IServiceCollection globalServices)
{
globalServices.AddNew(typeof(MyServiceClient), typeof(MyServiceClient));
}


I would create a wrapping 'Module Service' that wraps your WCF service. Also, if you are the owner of the service code, I would suggest using ChannelFactory<T> instead of generating a proxy.

Note this code is in you web client software factory module services and is registered in the module services / global services container (and maybe my choice of interface name/class name conflicts with your WCF service contract name) .
    public interface IMyService{ string Operation(string parameter); }
 
    public class MyServiceWcfClient : IMyService
    {
        public string Operation(string parameter)
        {
            try
            {
                // we'll use the generated proxy here
                MyServiceClient client = new MyServiceClient();
                string result = client.Operation(parameter);
                return result;
            }
            catch (Exception)
            {
                // TODO catch more specific exceptins
            }
        }
    }


Example of using the ChannelFactory<T> class
    ChannelFactory<IMyService> factory = new ChannelFactory<IMyService>("EndpointName");
    IMyService service = factory.CreateChannel();
    // now you can call your operations on your service
 
Apr 2, 2008 at 11:31 AM
I still add a service reference to my WCSF solution when going for the factory-solution of yours, right? Else I wouldn't get get the IMyService interface and type information from the service, right?

Where do I do the factory-creation-stuff? I a new class that I call something like MyServiceProxyWrapper? Should it inherit from some baseclass? What if I need state in my service? I have a connection to a COM-server within my service so I need the state.

Could you explain why you would use ChannelFactory instead of a proxy? I have read about ChannelFactory in the context of handling timeouts and exceptions. Is that why you prefer the factory-way?
I would also like to hear from you on how this ChannelFactory could be implemented to handle those exceptions...... I guess if a channel is getting into a faulted state you just create a new channel, but what about my state then?

TIA

RightCoder
Apr 21, 2008 at 1:42 PM
I understood the wrapping of WCF Service (IMyService) as this layer shall handle all the Exception, Logging and Caching.

What i used to do was,

globalServices.AddNew(typeof(MyServiceClient), typeof(MyServiceClient));

and then i used to use ServiceDependency where ever i used to require the service in my client. Even my services shall require some objects whose state has to be preserved. Will ChannelFactory create a enw instance of the service for each call? And is it advisable to create it? In the mean time will read as much as i can about ChannelFactory.

Thanks and Regards,
Wali.