How to call web services from WSDL? - c#

I have save the wsdl into my local computer, so I have create a new project with visual studio (c#) then I have import this file. So I have the WSDL into my project and I have PianoResidentialService under "Service Refereces" folder.
Now I want to try to call a web service. But I don't know how do this.
I have try this code but now works.
public MainWindow()
{
InitializeComponent();
PianoAssistenzialeResidenzialeService.getPianoAssistenziale ws_PA = new PianoAssistenzialeResidenzialeService.getPianoAssistenziale();
}
If I try to get Inspect ws_PA, all field is null.

You probably need to specify the endpoint:
ws_PA.URL = "http://some.server.com/endpoint/";
Then, you should call a method which is generated for the operation you want to call. (Remember that a webservice can have multiple operations.)
var result = ws_PA.Operation(parameters);
You might want to share the WSDL with us and tell us in more detail what you want to achieve.

Related

Using a "shared" type on a WCF/MVC project with two services ("cannot convert from...")

I've two WCF services connected to my client. I want to use a User-object, retrieved from service #1, and use this as paramter for service #2. Here is my MVC-Controller TournamentController.cs code:
private readonly GCTournamentServiceClient _tournamentClient = new GCTournamentServiceClient();
public ActionResult Join(int id)
{
GCUserServiceClient userClient = new GCUserServiceClient();
// Get current user
var currentUser = userClient.GetUser(0);
if (currentUser != null)
{
// Get selected tournament
var selectedTournament = _tournamentClient.GetTournament(id);
// Check if there are available seats in the tournament
if (selectedTournament.Seats > selectedTournament.RegistredUsers.Count)
{
// Check if user exist amoung registred users
if (!selectedTournament.RegistredUsers.Contains(currentUser))
{
selectedTournament?.RegistredUsers.Add(currentUser);
}
}
}
}
The error Visual Studio prompt me with:
Argument 1: cannot convert from 'GConnect.Clients.WebClient.GCUserService.User' to 'GConnect.Clients.WebClient.GCTournamentService.User'
So the problem is currentUser, which has the type GCUserService.User. I'm unable to use this as parameter for RegistredUsers
The error itself makes perfect sense, however, I'm not quite sure how I'm suppose to convert this type (properly). Some articles states, that a "shared"-service has to be created, which holds the User-type. I just can't believe, that a solution like that, should be necessary.
I might have misunderstood some basic stuff here (working with WCF and MVC), but please enlighten me, if that's the case.
So the problem is currentUser, which has the type GCUserService.User.
I'm unable to use this as parameter for RegistredUsers
There are 2 approaches to solve this problem:
1)
Create a class library project (Visual Studio) and add the User class in that project, compile it and add its assembly's (.dll) reference to both services and the client (your MVC application). Next retrieve that user object as you are already doing it
var currentUser = userClient.GetUser(0);
GetUser will return the type of User that is defined in a separate assembly which is added as reference as suggested above. The TournamentService will also reference the same assembly and the RegistredUsers.Add(User userToadd) method will take the same User object and WCF runtime should be able to serialise/desterilise it.
2)
In your MVC client application, new up the User object that is acceptable by the TournamentService.RegistredUsers.Add method. Populate its properties from the currentUser and pass in that object as parameter to RegistredUsers.Add method.
Best Practice
Ideally, I would recommend the first approach which is more work but a better practice and that your User class is maintained centrally and code is reused.
Hope this helps!

Why the result from the web service references and Service references are different?

I am bit curious about one thing which has happen while trying to understand the concept of Service References and Web Service References.
What I did is?
In my project I have added a web service as a Service Reference and trying to get my script run through the use of client.
But while getting result it is throwing an exception as in the following image:
I have tried to trace out the cause but not able to get the proper answer for that.
I have following code for the resultant object.
[
ComVisible(false),
Serializable,
SoapTypeAttribute("RecordList", "http://www.someadd.com/dev/ns/SOF/2.0"),
XmlType(TypeName="RecordList", Namespace="http://www.someadd.com/dev/ns/SOF/2.0")
]
public class MyRecordListWrapper
{
private IxRecordList recordList = null;
private const string XMLW3CSchema = "http://www.w3.org/2001/XMLSchema";
[SoapElement("Headers")]
public Header[] Headers = null;
[SoapElement("Records")]
public Record[] Records = null;
// some methods to work on intialization
public SmRecordListWrapper(ref IxRecordList p_RecordList)
{
recordList = p_RecordList;// record list initialization
Headers = CreateWrapperHeaders(); // will return header class object
Records = CreateWrapperRecords(); // will return record object
}
}
Can anyone tell me why this error is showing for me?
While adding reference as a Web Service Reference
when I add the same reference as a web reference that time the program is not showing any error and runs successfully?
So can anyone tell me what is the difference in working with the same code using service reference and web service reference?
and Which is a correct way to ass references?
Hope I will get some more described answers to make the things easy to understand.
Thanks in advance.
Adding a web reference, visual studio uses xsd.exe to generate the classes from the service metadata. This uses XmlSerializer under the hood.
Adding a service reference, visual studio uses svcutil.exe to generate the classes from the metadata. This uses DataContractSerializer under the hood.
Two separate tools, two outcomes. For general information, DataContractSerializer is a lot less forgiving when it comes to generating classes from metadata.

Do I need to change web service path everytime when I call it?

I am developing on Windows application in c# and I am using web server's web service in this Windows application.
The web service should be dynamic and I need to change it in the application.
I managed to do it by this code :
CallWebService.MyWS ws = new CallWebService.MyWS();
ws.Url = "new url";
This new url will be set as per client's web server url.
I am calling this web service (I mean web service functions) 20 to 25 times in my application.
Do I need to change this path everytime when I call it or for the first time will be ok ?
Use a fixed port number for your service and configure this url in your app/web.config file and use it in your code.
Create a helper class and use that. Make it configurable by using an app setting or better store in config table in database if you are using one.
If you are using WCF client, you can pass URL in client constructor. Otherwise create a partial class for your webservice to create that constructor.
public class MyWebServiceHelper
{
private string _url = null;
public MyWebServiceHelper()
{
this._url = GetWsUrlFromDbOrAppConfig();
}
public CallWebService.MyWS GetMyWebServiceProxy()
{
return new CallWebService.MyWS("WcfBindingConfig", _url);
}
}

Add web reference to silverlight application

I'm new to silverlight, I want to add web reference to silverlight application is that possable???
I can only add service reference to the SilverightApplication but i want to add web reference.
I can add web reference to the SilverightApplication.Web, can i use it from SilverightApplication??
I aslo can add service reference to the SilverightApplication but functions of service reference has no return value so i cant recieve the data, here is the code
Service1SoapClient c = new Service1SoapClient();
ServiceReference1.Service1SoapClient a = new ServiceReference1.Service1SoapClient();
a.returnStr_19_9Async("");
the function returnStr_19_9Async("") has no return value can any one please tell me what is teh wrong??
Can you please tell me how, please explain..
Thanks.
The problem you may be striking is that WCF calls are all done asynchronously in Silverlight. This means when you call your initial service function (let's call it GetMyClient(int clientId) ), the proxy that you have generated will have a function called GetMyClientAsync(), and it will also have an event called GetMyClientCompleted which you must subscribe to:
myProxy.GetMyClientCompleted += new EventHandler<GetMyClientCompletedEventArgs>(MyProxy_GetMyClientCompleted);
that event handler will look something like this:
private void MyProxy_GetMyClientCompleted(object sender, GetMyClientCompletedEventArgs e)
{
//e.Result will have your returned values
}
This is just a very brief overview that gives you enough to get started. You could also read more here: Silverlight.net | Data & Networking | Network Services (Soap, REST and More)

WCF does not generate the properties

I have a .NET 1.1 ASMX and want to use it in a client WinForms app.
If i go wit the old way and add it as a "WebRefrence" method then I will have access to two of its properties which are "url" and "UseDefaultCredentials" and it works fine.
But if I go with the new WCF way and add it as a ServiceReference I still have access to the methods of that ASMX but those two properties are missing.
what is the reason for that?
so for example in the old way ( adding WebReference) these codes are valid:
TransferService transferService= new TransferService();
transferService.Url = "http://something.asmx";
transferService.Credentials = System.Net.CredentialCache.DefaultCredentials;
string[] machines = transferService.GetMachines();
But in the new way ( adding Service Reference )
using(TransferServiceSoapClient transferServiceSoapClient = new TransferServiceSoapClient("TransferServiceSoap"))
{
transferServiceSoapClient.Url = "someUrl.asmx"; //Cannot resolve URL
transferServiceSoapClient.GetMachines(new GetMachinesRequest());
transferServiceSoapClient.Credentials = .... // //Cannot resolve Credentials
}
Because those are configured in the endpoint in your app/web.config or programatically if you prefer. More on configuring a WCF client here.

Categories

Resources