My WCF contains two methods. The first is simple which returns string and takes string as parameter, but in second method I am passing a class containing properties as parameter and return string.
When I comment second method then I am able to get WCF_Masters.ServiceClient object in client application but after uncommenting 2nd method I am unable to get that object.
I get only CompositeType instead of ServiceClient of My WCF service whose name is WCF_Masters.
Note that I am trying to consume WCF in WPF Windows application.
How can I get rid of this issue?
Edit
My WCF methods are:
public String GetMessage(string vName)
{
return "Hello world from " + vName;
}
public String SaveEmployee(EmployeeMasterSC vEmployeeMasterSC)
{
String mReturnMsg = string.Empty;
EmployeeMasterDAL vEmployeeMasterDAL = null;
vEmployeeMasterDAL = new EmployeeMasterDAL();
mReturnMsg = vEmployeeMasterDAL.SaveEmployee(vEmployeeMasterSC);
//mEmpDset.EmployeeData = Mdset; return mReturnMsg;
}
If I understood you correctly, you should not be adding a reference to your WPF DLL in your WCF service. Define the model in your service and then instantiate that model (by reference) in your WPF application, which is consuming the service.
I believe the reason it works when you comment out the 2nd method is that your 1st method has no reference to class structures defined in the WPF DLL.
Got the solution guys, It was the issue of database name I have Written incorrectly "AirportPortal" instead of "DB_AirportPortal" in WCF while inserting records into database
Related
I'm creating an UWP app, that needs to connect to asmx service. I've done that by using service reference, which works with TempConvert but not with my asmx service
This is how my code looks like
HelpdeskSoapClient HPsoapClient = new HelpdeskSoapClient();
getIncInput input = new getIncInput();
input.username = "450";
getIncResponse realresponse;
realresponse= await HPsoapClient.getIncAsync(input);
Debug.Write(realresponse);
but response is not an XML file or something else, but "DTS.helpdesk.getIncResponse"
Any clues ?
getIncResponse is the object that is returned by the service when you make the call.
When you pass realResponse into the Debug.Write, since it's an object, the base functionality of ToString() is called, which unless overridden in the object will return the fully qualified type name - i.e., DTS.helpdesk.getIncResponse.
You should be able to access the properties of the object to get the data, similar to how you set the username on the getIncInput object (input.username = "450";).
The temperature conversion service worked because it returns a simple type (string), not a complex type (getIncResponse). There is no error here.
I have written a basic WCF service that uses SubSonic for data retrieval.
After publishing the service I am consuming it in a C# application. When calling the method that uses that SubSonic query, I get back the right number of objects from the database, but none of them contain the database properties and their values. It looks like only SubSonic properties.
The SubSonic DAL is contained in a separate project that is referenced in the WCF service project.
WCF service interface:
[OperationContract]
GeoLocationCollection GetGeoLocations(long websiteID);
Worker method:
public GeoLocationCollection GetWebsiteGeoLocations(long websiteID)
{
GeoLocationCollection locationsCollection = new Select()
.Where(GeoLocation.Columns.WebsiteID).IsEqualTo(1)
.From(GeoLocation.Schema)
.ExecuteAsCollection<GeoLocationCollection>();
return locationsCollection;
}
Both the GeoLocationCollection and GeoLocation have been automatically decorated with [Serializable].
The service is consumed as follows:
MyService.MyServiceClient client = new MyService.MyServiceClient();
var result = client.GetWebsiteGeoLocations(1);
foreach (MyService.GeoLocation location in result)
{
// do stuff
}
So once again, why can I not see any of my actual table properties/values in location?
WCF services require [DataContract] + [DataMember] attribute and not [Serializable]. This maybe the reason for you to not get the attribute values.
I have a followup question to this question.
I'm writing a web service which dynamically calls other web services, using the WSProxy class found here.
Using WSProxy returns an object with a dynamic type, depending on the web service method called. For example, if I'm calling a method that returns...
<StateCodes>
<StateCode>
<Code>AL</Code>
<Name>Alabama</Name>
</StateCode>
<!-- and so on -->
</StateCodes>
then the object is of the type StateCodes[].
If I'm calling a method that returns ...
<GetVehicleMakes>
<VehicleMakes>
<Vehicle_Make_Code>00</Vehicle_Make_Code>
<Vehicle_Make_Description>Ford</Vehicle_Make_Description>
</VehicleMakes>
<VehicleMakes>
<Vehicle_Make_Code>01</Vehicle_Make_Code>
<Vehicle_Make_Description>Toyota</Vehicle_Make_Description>
</VehicleMakes>
<!-- and so on -->
</GetVehicleMakes>
then the object is of the type GetVehicleMakes[].
I can't declare a class type beforehand because the class type of the returned object is determined by the web service method called, and the web service method is determined at runtime. There are dozens of methods with different return types on the local service I'm testing against. I don't get to know the type of the returned object before runtime, because any method from any web service could be called.
When I try to return the object straight up, like so:
[WebMethod]
public object RunService(string webServiceAsmxUrl, string serviceName, string methodName, string jsonArgs)
{
WSDLRuntime.WsProxy wsp = new WSDLRuntime.WsProxy();
// Convert JSON to C# object.
JavaScriptSerializer jser = new JavaScriptSerializer();
var dict = jser.Deserialize<Dictionary<string,object>>(jsonArgs);
object result = wsp.CallWebService(webServiceAsmxUrl, serviceName, methodName, dict);
// This line produces the error.
return result;
}
I can insert a breakpoint at the return result line, and browse my result object. For example, when I call the StateCodes method, the result variable is the StateCodes[] array.
However, once return result runs, the XML parser won't have it.
System.InvalidOperationException: The type StateCodes[] may not be used in this context.
I've searched for answers, and I see terms like, "reflection" and "serialization" coming up, but I am very new to C# and don't know if these are what I want or how they work.
I'm using C# 3.5.
It looks like you need to return the xml that is returned from the service call. You can't return the object directly because the top-level service (the one that has a return type of object) is not generated at runtime; it is defined when its wsdl is consumed.
Long story short, you should change the return type of your top-level WebMethod to be string and serialize result manually in order to do what you want. Then the client (who presumably knows what they expect to get back from the service that they are requesting via webServiceAsmxUrl, serviceName, and methodName) can deserialize the result themselves.
I'm having a problem passing an object for some reason and I'm not sure why. I'm getting a Object reference not set to an instance of an object error.
Essentially from my client application I make a call to a Windows Work...
Client Code
Workflow1Client client = new Workflow1Client();
ACME.Order newOrder = new ACME.Order();
newOrder.Property1 = "xyz";
//set all the other properties
string status = client.GetData(newOrder);
//**This is where object reference error occurs**
Proxy Expecting
public string GetData(ACME.Order NewOrder)
{
return base.Channel.GetData(NewOrder);
}
Workflow Code
[ServiceContract]
public interface IWorkflow1
{
[OperationContract]
string GetData(ACME.Order NewOrder);
// TODO: Add your service operations here
}
I'd appreciate any help on this. Also beyond this question is sending a Object (ACME.Order) good practice or should I be trying to tackle this a different way?
Thanks
I have run into this myself and in my case it was a Serialization error on the custom object. To be able to send a custom object across WCF it should have the [Serializable] attribute. To test, see if you can serialize the object to an XML file. If that fails the WCF transfer will not work.
Hope that helps.
I have created a SoapExtension class to capture the soap request and response from specific web service calls. In order to put this SoapExtension into effect, I have to add an attribute to the method in the generated proxy client.
For example, I've added the AuditSoapCapture attribute to this method:
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://blahblah.com/webservices/AddressSearch", RequestNamespace = "http://blahblah.com/webservices/", ResponseNamespace = "http://blahblah.com/webservices/", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[blahblah.TriadLite.Core.AuditSoapCapture]
public System.Data.DataSet AddressSearch(string HouseNumber, string StreetName, string ZipCode) {
object[] results = this.Invoke("AddressSearch", new object[] {
HouseNumber,
StreetName,
ZipCode});
return ((System.Data.DataSet)(results[0]));
}
I am looking for a way to add this attribute to specific methods without modifying the generated client proxy, as they will get lost when we regenerate. Can I do this in a another partial class or interface or some other way?
Thanks!
Unfortunately, you'll need to modify the proxy code. The other possibilities you mention will not work - a parial class will not overwrite existing functionality, and there is no way that I'm aware of getting an interface to do what you need (compounded by the fact that there is no way to even let the proxy generator know that you intend to implement an interface).
Something that I've done in the past, in a situation where you have access to the source of the webservice, is to write a little app that will parse the code (as text) in the .asmx.cs file of the webservice to extract the names of all the methods that are tagged with [WebMethod]. Then the app "fixes up" the References.cs by inserting appropriate attributes onto the proxied methods, based on some settings file or somesuch. This works well because the naming conventions in the proxy map very neatly to the method names in the original service.
I may just end up injecting my SoapExtension by putting it into the Web.config. This will cause it to be run on every WS call without a client proxy method attribute. Then, I will modify the SoapExtension to look up the called WS method name on a list, and if it is on the list, then do the rest of the SoapExtension logic. I figure the hit on the list in this small volume application isn't going to kill performance.
6 years ago this was posted... So not sure if this will help anyone at this point.
I ran into something similar with a call to an old SOAP web service that had a dynamically generated proxy class that we didn't want to modify as it was auto-generated from the wsdl by the project. In order to solve this problem here is what we did.
The proxy class generated by wsdl.exe is a partial class. We extended this class like so to add a property with the information we wanted to access in the soapextension. You can add as many properties as you want...
partial class mysoapwebservice
{
public string myproperty{ get; set; }
}
in the web.config we registered the soap extension globaly on the project
<webServices>
<soapExtensionTypes>
<add type="MySoapExtension" priority="1" group="Low"/>
</soapExtensionTypes>
</webServices>
In the code were we created the web service object 'mysoapwebservice' we set the value of the property we needed.
In the soapextension you can get a reference to the web service that was called as well as the values. You can also determine the method call.
`
public class MySoapExtension: SoapExtension
{
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
{
// web service client object
var webserviceobject= ((SoapClientMessage)message).Client;
// method from web service that was called
var calledMethod = (SoapClientMessage)message).MethodInfo;
// checked the client type of webserviceobject and
//added method / property specific logic here
}
}
}
// other soap extension code
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class CriptoExtensionAttribute : SoapExtensionAttribute
[CriptoExtension]
public partial class MainService{