Nested List as a Parameter in WCF web service - c#

I have a WCF method which have a nested List parameter, like this
public void Method(List<class1> class1Obj, List<List<SomeClass>> someClassObj)
{
// CODE
}
After setting service reference I get this in my client reference method through which I can call my WCF method
public void Method(class1[] class1Obj, SomeClass[][] someClassObj)
{
base.Channel.Method(class1Obj, someClassObj);
}
Now to call this method from my code I can do this
void myServiceCaller()
{
List<class1> class1Obj = new List<class1>();
// Add items to class1Obj
List<List<SomeClass>> someClassObj = List<List<SomeClass>>();
// Add items to someClassObj
ServiceRef.myServiceClient service = new ServiceRef.myServiceClient();
service.Method(
class1Obj.ToArray(), // This one is fine
someClassObj.ToArray() // This gives me compile time error
);
}
How can I resolve this issue to convert List<List<SomeClass>> to SomeClass[][] ?

When you add your Service Reference and the dialog pops up you can click the Advanced... button in the lower left and change the Collection type drop-down from System.Array to System.Collection.GenericList this will then change the proxy that is created and use List<...> instead of [...] when collections are used.
In addition, if you have already added your Service Reference you can right-click on the Service Reference within the Solution tree and click Configure Service Reference... from the context-menu. This will show the same "Advanced" dialog mentioned above.

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!

How to consume WCF after passing C# class object

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

Passing an Id from Android app to WCF Service App

I am trying to get a listview selected item to work. I am trying to see if I can pass the selected value to a wcf and query the database from a selected county to a town. I am just wondering how I can do this?
Here is my onitem click event that I am building at the minute:
public void OnItemClick(AdapterView parent, View view, int position, long id)
{
var selectedValue = parent.GetItemAtPosition(position);
var Intent = new Intent(this, typeof(SelectLocationActivity));
// selectedValue should already be a string but...
Intent.PutExtra("Name", selectedValue.ToString());
StartActivity(Intent);
}
I am just wondering how I can implement the calling of the wcf service app and post the selected value to it. I am using Xamarin by the way
If you have set up your webservice, then just add a Service Reference in your project and all the exposed functions of your ws will be available through the proxy class which will be created when you added the reference.

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)

add a web service reference to a console app

Im creating a simple web service in a console app. (PersonService)
this is my Program.cs below
im trying to add a service reference to a different console app (PersonClient)
how can i do this?
i tried adding it by right clicking, add service reference, pointing to the refernce etc...
but it wont work.
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
[ServiceContract]
public interface IPersonLookup
{
[OperationContract]
Person GetPerson(int identifier);
}
public class PersonService : IPersonLookup
{
public PersonService()
{
}
public Person GetPerson(int identifier)
{
Person p = new Person();
p.FirstName="Jane";
p.LastName="Doe";
return p;
}
}
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(PersonService)))
{
WSHttpBinding binding = new WSHttpBinding();
host.AddServiceEndpoint(typeof(IPersonLookup), binding, "http://localhost:9090/PersonService");
host.Open();
Console.WriteLine("Listening....");
Console.ReadLine();
}
}
}
Solution:
Create a console application using visual studio.
Right click on the project and click on "Add Service Reference...".
On the window, you will find the "Advanced" button at the bottom.
Click on the button and it will open service reference settings window. It has a button at bottom called "Add Web Reference".
You need to read about WCF MEX Endpoints. Here's a blog post that may help.
You have two console exes, one which runs a ServiceHost - is that correct? Run the server console without debugging; then in the IDE add the WCF reference to the url. It should work, but it needs the server (your second console exe) to be running when you query the mex.
When you added the webservice reference, you defined the namespace and 'class name' for the service. You must either add the namespace reference ("using FooNameSpace;") or use the fully qualified class name of the service ("FooNameSpace.BarClass ws = new FooNameSapce.BarClass()");
Create a console application.
Right click on the References and click on Add Service
Reference.
Click on Advanced button at the bottom.
In the New Window click on Add Web Reference.
No one mentioned yet that you need a couple things before you can use "Add Service Reference".
Use the Visual Studio 2019 Installer tool to modify your existing installation.
Check the ".NET Desktop Development Workload". (This will add a second type of console app.)
Now launch VS 2019 and then create a project using: Console App (.NET Framework) IMPORTANT: Do not select the one for .NET Core, or it will not have the "Add Service Reference" option!
Now go to the main menu bar and select Project -> Add Service Reference. Now you can add your reference.
Happy coding!

Categories

Resources