Reputation: 11607
I am developing a very simple WCF Example, but I am dismally failing into retrieving, in server side, the string value I am sending from client side. My code is very basic though. If anyone could lead me to the tracks where to look at to fix this I would be very grateful (And please forgive my limited WCF knowledge)
EDIT : Funny Part is that I am able to retrieve a return value in client back from server !
(see modified code below)
Client code :
public void SendData()
{
string rogerback = proxy.SendData("String To Be Delivered");
Console.Writeline(rogerback); //<--Prints "PICKABOO"
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IServiceOrder")]
public interface IServiceOrder
{
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IServiceOrder/SendData")]
string SendData(string data);
}
Server Code :
[ServiceContract]
public interface IServiceOrder
{
[OperationContract]
string SendData(string data);
}
public class ServiceOrder : IServiceOrder
{
public string SendData(string value)
{
if (value== null) Console.WriteLine("value IS NULL"); //<--Always the case when I execute Client code SendData()
return "PICKABOO";
//return value <-- returns null
doSomething(value);
}
}
Upvotes: 4
Views: 7097
Reputation: 1
I think the problem is that you forgot to include the attribute [DataMember] I suggest that you use proper class object to serve as your parameter. In that way it represented in a well formed object.
Upvotes: 0
Reputation: 1
If you have already added a service reference, delete that service reference completely. add a service reference newly to your client application and run the application.
The reason for the old reference name is, whenever we create a web service reference, a reference file will be created. Eventhough we update/ configure the web service the old reference name will be sustained in the reference file. When you completely delete and add the service reference a new reference file will be created, there after the values will not be null. This is the solution
Upvotes: 0
Reputation: 4662
though I dont know what is your issue may be something wrong with your generate code Here is full working example for sending a string from client and then receiving it from server.
using System;
using System.Runtime.Serialization;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace MySpace
{
[DataContract]
public class Data
{
[DataMember]
public string MyString;
}
[ServiceContract]
public interface IService
{
[OperationContract]
Data Method(Data dd);
}
public class Service : IService
{
public Data Method(Data dd)
{
dd.MyString = dd.MyString + " String from Server.";
return dd;
}
}
class Program
{
static void Main(string[] args)
{
string Url = "http://localhost:8000/";
Binding binding = new BasicHttpBinding();
ServiceHost host = new ServiceHost(typeof(Service));
host.AddServiceEndpoint(typeof(IService), binding, Url);
host.Open();
ChannelFactory<IService> fac = new ChannelFactory<IService>(binding);
fac.Open();
IService proxy = fac.CreateChannel(new EndpointAddress(Url));
Data d = new Data();
d.MyString = "String from client.";
d = proxy.Method(d);
fac.Close();
host.Close();
Console.WriteLine("Result after calling \n " + d.MyString);
Console.ReadLine();
}
}
}
Update: ran code without DataContract by just passing string it works fab
using System;
using System.Runtime.Serialization;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace MySpace
{
[ServiceContract]
public interface IService
{
[OperationContract]
string Method(string dd);
}
public class Service : IService
{
public string Method(string dd)
{
dd =dd+ " String from Server.";
return dd;
}
}
class Program
{
static void Main(string[] args)
{
string Url = "http://localhost:8000/";
Binding binding = new BasicHttpBinding();
ServiceHost host = new ServiceHost(typeof(Service));
host.AddServiceEndpoint(typeof(IService), binding, Url);
host.Open();
ChannelFactory<IService> fac = new ChannelFactory<IService>(binding);
fac.Open();
IService proxy = fac.CreateChannel(new EndpointAddress(Url));
string d = proxy.Method("String from client.");
fac.Close();
host.Close();
Console.WriteLine("Result after calling \n " + d);
Console.ReadLine();
}
}
}
Update 3 I still believe there was something wrong with your generated code/proxies as here is test with different interfaces on client/server ( Any way you can ignore it, if your problem is already solved :)
using System;
using System.Runtime.Serialization;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
namespace MyClient
{
[ServiceContract]
public interface IService
{
[OperationContract]
string Method(string dd);
}
}
namespace MyServer
{
[ServiceContract]
public interface IService
{
[OperationContract]
string Method(string dd);
}
}
namespace MySpace
{
public class Service :MyServer.IService
{
public string Method(string dd)
{
dd =dd+ " String from Server.";
return dd;
}
}
class Program
{
static void Main(string[] args)
{
string Url = "http://localhost:8000/";
Binding binding = new BasicHttpBinding();
ServiceHost host = new ServiceHost(typeof(Service));
host.AddServiceEndpoint(typeof(MyServer.IService), binding, Url);
host.AddDefaultEndpoints();
host.Open();
ChannelFactory<MyClient.IService> fac = new ChannelFactory<MyClient.IService>(binding);
fac.Open();
MyClient.IService proxy = fac.CreateChannel(new EndpointAddress(Url));
string d = proxy.Method("String from client.");
fac.Close();
host.Close();
Console.WriteLine("Result after calling \n " + d);
Console.ReadLine();
Console.ReadLine();
}
}
}
Upvotes: 2
Reputation: 11607
Apparently, there is a misconception from my side, concerning how WCF interfaces work.
As you can see in my code, interface "IServiceOrder" is defined twice. Both on Client AND server side.
Surjit Samra's code contained only one definition for the interface, and Ralf's Sudelbücher's example defined it only once in "WCFSimple.Contract" namespace.
The fact that the interface was defined twice was what caused the service to "fail" in a very peculiar way.
If somebody could give a thorough explanation on why this double interface definition causes this behaviour, that would be good.
Basically, I've chosen to do so because I wanted my client to be totally agnostic of the server implementation. But I was mislead, as both client and server need to see AND access a single interface (i.e contract) definition. I guess that's how it works.
Upvotes: 1