Passing Entity on ASP.NET Web Service - c#

This might be very easy, but What I am trying to do here is, I am trying to pass a Customer Object to a Web Service Method. Customer class is on Entity namespace and it is serializable, and I am adding reference to both of my ASP.NET application which calls web service and pass Entity.Customer Object and also in WebService which accepts Enity.Customer Object.
Web Service Method
[WebMethod]
public void AddCustomer(Entity.Customer c)
{}
ASP.NET Applcation
Entity.Customer c = new Entity.Customer;
webservice.AddCustomer(c);
Error
The best overloaded method match for 'TestApplication.localhost.Service1.AddCustomer(TestApplication.localhost.Customer)' has some invalid arguments
I tried changing the web service to accept Object and later cast that object to Customer.Entity, the Application compiles but I was getting XML generation errors.

Are you generating the web service method using the 'Add Web Reference' method from within Visual Studio?
This is a compile-issue right? You application doesn't want to compile? Just go to the definition of the method and make sure you are passing the same customer object as specified in the method definition - usually the generated web service method generates a proxy version of the object and you need to pass that exact same class.

Often the helper methods generated through a service method expect the local generated objects - this is because some elements of the server-side objects aren't serialized, or are handled slightly differently because of the serialization.
Try:
TestApplication.localhost.Customer c = new TestApplication.localhost.Customer();
webservice.AddCustomer(c);
This way you're using the generated objects, with the correctly serialized properties, rather than the original object.

Related

How to use SOAP service generated class methods?

I have created a SOAP service.
Now i want to consume it in a c# client application. I added the service using 'Add service reference' and service reference is added to client.
All my service entities are in service. And in current scenerio i can't move them to a common library.
Problem is, my service endpoint is accepting List<Foo> as parameter.
Foo has a method Boo.
In client, when i try to Foo.Boo() i get Cannot resolve symbol Boo error.
Unfortunately only methods on the service itself are exposed via a SOAP web service, methods on objects used as parameters or return values are not. If the method relates to a server-side operation then you could expose it at the service root level taking the instance object as parameter, or if it relates to a client-side operation you could consider adding it as a client-side extension method.

Call a soap client's method using a string and an object

I'm using an external open source project that provides me computation services that I create using a UI that it provides.
The project creates web-service endpoints automatically that I'm suppose to consume via my application.
My problem is that I can't interfere in the process. It's a black box that creates a service for me when I choose to deploy the project.
Each service has a bunch of different logical "private methods" that are exposed in the wsdl that's automatically created.
If I could create the service myself, I would create one an interface with an exposed method called Process that will have one general input request param and one general Response, something like:
public GeneralResponse Process(GeneralRequest request);
I want to create a generic out-point in my application which passes two parameters:
1.Endpoint Url to shoot the request to.
2.Generic request as an input param.
I'm using C# and the easiest way to consume a service is simply adding a service-reference, creating a client and calling the wanted method.
Since I don't want to add a client per service reference, I'll add a random one, change the client's endpoint address and shoot the request.
The problem with this approach is that the client generated will expose it's "private methods" and I don't want other programmers on my team to accidentally invoke them.
Bottom line:
Is there any elegant way to create a Generic soap client and invoke a method using a string? Similar to the way you call the Invoke method when you use reflection?
Something like this:
GenericRequest req = GetRequest();
SoapClient client = new SoapClient(endpointUrl);
GenericResponse res = client.Invoke("Process", req) as GenericResponse;

no overload method takes 2 arguments

Hi I have a WCF service and within it I have this method
void SendData(int pumpNo, List<String> pumpInfo);
however when I try to pass an int and a list into it, I get an error saying
Error 1 No overload for method 'SendData' takes 2 arguments
This is how I passed data to it in the WCF client
sendpumpdata.SendData(pumpID, pumpData);
ok so at the top I create an instance of the WCF service by doing...
ServiceReference1.iCommClient sendpumpdata = new Pumps.ServiceReference1.iCommClient();
also in my service.cs I have created the method defined in the IService.cs
A WCF web service leverages client generated code (i.e. a proxy) to communicate with the server. In your situation, even though the server code has two parameters, your client generated code must be out of date.
If you're using a Web Reference or a Service Reference just right click and Update Reference. If you're using a static WSDL then navigate to the WSDL hosted locally for the WCF service and save it to disk and then overwrite the one in your project.

Default property values for WCF client

I have the following scenario:
There is a complex object that is residing in the standalone class library and some of the object properties have default values.
The object is argument for WCF public method.
I instantiate this object on the client of WCF, assign values to properties and pass it to the WCF public method.
The WCF method on service side accepts it and does whatever.
My problem is that when I instantiate the object on the client property default values are not available for me to use and I have to assign them in code again.
I looked through the past questions on the topic here and did not find anything related to my scenario. I don't really have data contract for the argument although on the client my object gets instantiated not from the class library itself but from the service reference, like WCFServiceReference.MyClass (otherwise WCF method can not accept it as argument).
I would really like to have all those default values to be available on the client.
I would appreciate any assistance.
Thanks!
With your approach -- using the service proxy classes -- the client only gets the properties marked with DataMember. That's why the default values you assign don't appear in the client -- that code is not serialized, so it's not sent with the WCF service.
If you want to share code, you can do it by declaring your DataContract classes in a separate class library. Have the WCF service and the client both reference that library.

Sending SOAP requests in C#

I am trying to consume a SOAP service in C#, so I added my WSDL as a Service Reference. So far, I have created an instance of the request I want to send, but I don't know how to send it, or process the response.
Can someone explain how to do this?
When you added the service reference, Visual Studio should generate some code for you, including a class for the service which is in its own namespace.
So, you need to create a new instance of this service:
var oService = new ServiceNamespace.ServiceClient();
Then you can call your methods on the service:
oService.SomeMethod();
here you can find the full documentation and sample:
http://msdn.microsoft.com/en-us/library/aa529276.aspx
Here is a full example of How you create a WebService and How to consume it. As I see you just need the part of how to consume it. But it is like a normal call function you send parameters and receieve a result parsed to an object. Sometimes Value Objects created by the Service Reference Tool. Hope it helps.
By the way it uses the Web reference, with a service reference is quite similar just the name of your Class is parsed with a SoapClient at the End, lets say that your service is named Foo, the Service reference will generate it for you like FooSoapClient

Categories

Resources