C# WCF:服务接收“;空";发送完整字符串后的字符串
我正在开发一个非常简单的WCF示例,但在服务器端检索从客户端发送的字符串值时,我失败了。我的代码非常基本。如果有人能带我找到解决这个问题的方法,我将非常感激(请原谅我对WCF的了解有限) 编辑:有趣的是,我能够从服务器检索客户端中的返回值C# WCF:服务接收“;空";发送完整字符串后的字符串,c#,wcf,C#,Wcf,我正在开发一个非常简单的WCF示例,但在服务器端检索从客户端发送的字符串值时,我失败了。我的代码非常基本。如果有人能带我找到解决这个问题的方法,我将非常感激(请原谅我对WCF的了解有限) 编辑:有趣的是,我能够从服务器检索客户端中的返回值(参见下面修改的代码) 客户端代码: public void SendData() { string rogerback = proxy.SendData("String To Be Delivered"); Co
(参见下面修改的代码) 客户端代码:
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);
}
[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);
}
}
public void SendData()
{
string rogerback=proxy.SendData(“要传递的字符串”);
Console.Writeline(rogerback);//虽然我不知道您的问题是什么,但生成代码可能有问题
下面是从客户端发送字符串,然后从服务器接收字符串的完整工作示例
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();
}
}
}
使用系统;
使用System.Runtime.Serialization;
使用System.IO;
使用System.ServiceModel;
使用System.ServiceModel.Channel;
命名空间MySpace
{
[数据合同]
公共类数据
{
[数据成员]
公共字符串MyString;
}
[服务合同]
公共接口设备
{
[经营合同]
数据方法(数据dd);
}
公共课服务:IService
{
公共数据方法(数据dd)
{
dd.MyString=dd.MyString+“来自服务器的字符串。”;
返回dd;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
字符串Url=”http://localhost:8000/";
Binding Binding=新的BasicHttpBinding();
ServiceHost主机=新的ServiceHost(类型(服务));
AddServiceEndpoint(typeof(IService)、绑定、Url);
host.Open();
ChannelFactory fac=新的ChannelFactory(绑定);
fac.Open();
IService proxy=fac.CreateChannel(新端点地址(Url));
数据d=新数据();
d、 MyString=“来自客户端的字符串。”;
d=代理方法(d);
fac.Close();
host.Close();
Console.WriteLine(“调用\n“+d.MyString后的结果”);
Console.ReadLine();
}
}
}
更新:在没有DataContract的情况下运行代码,只需传递字符串即可
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();
}
}
}
使用系统;
使用System.Runtime.Serialization;
使用System.IO;
使用System.ServiceModel;
使用System.ServiceModel.Channel;
命名空间MySpace
{
[服务合同]
公共接口设备
{
[经营合同]
字符串方法(stringdd);
}
公共课服务:IService
{
公共字符串方法(字符串dd)
{
dd=dd+“来自服务器的字符串。”;
返回dd;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
字符串Url=”http://localhost:8000/";
Binding Binding=新的BasicHttpBinding();
ServiceHost主机=新的ServiceHost(类型(服务));
AddServiceEndpoint(typeof(IService)、绑定、Url);
host.Open();
ChannelFactory fac=新的ChannelFactory(绑定);
fac.Open();
IService proxy=fac.CreateChannel(新端点地址(Url));
stringd=proxy.Method(“来自客户端的字符串”);
fac.Close();
host.Close();
Console.WriteLine(“调用后的结果\n”+d);
Console.ReadLine();
}
}
}
更新3我仍然认为您生成的代码/代理有问题,因为下面是在客户端/服务器上使用不同接口进行的测试(如果您的问题已经解决,您可以忽略任何方式:)
使用系统;
使用System.Runtime.Serialization;
使用System.IO;
使用System.ServiceModel;
使用System.ServiceModel.Channel;
使用System.ServiceModel.Description;
命名空间MyClient
{
[服务合同]
公共接口设备
{
[经营合同]
字符串方法(stringdd);
}
}
名称空间MyServer
{
[服务合同]
公共接口设备
{
[经营合同]
字符串方法(stringdd);
}
}
命名空间MySpace
{
公共类服务:MyServer.IService
{
公共字符串方法(字符串dd)
{
dd=dd+“来自服务器的字符串。”;
返回dd;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
字符串Url=”http://localhost:8000/";
Binding Binding=新的BasicHttpBinding();
ServiceHost主机=新的ServiceHost(类型(服务));
AddServiceEndpoint(typeof(MyServer.IService)、绑定、Url);
AddDefaultEndpoints();
host.Open();
ChannelFactory fac=新的ChannelFactory(绑定);
fac.Open();
MyClient.IService proxy=fac.CreateChannel(新端点地址(Url));
stringd=proxy.Method(“来自客户端的字符串”);
fac.Close();
host.Close();
Console.WriteLine(“调用后的结果\n”+d);
Console.ReadLine();
Console.ReadLine();
}
}
}
显然,我对WCF接口的工作方式存在误解
正如您在我的代码中看到的,接口“IServiceOrder”在客户端和服务器端定义了两次。
Surjit Samra的代码只包含一个接口定义,并且在“WCFSimple.Contract”命名空间中只定义了一次。
接口被定义了两次,这是导致服务以一种非常特殊的方式“失败”的原因。
如果有人能彻底解释为什么这个双接口定义会导致这种行为,那就太好了
基本上,我选择这样做是因为我希望我的客户机对服务器实现完全不可知。但我被误导了,因为客户机和服务器都需要查看和访问一个单一的接口(即合同)定义。我想这就是它的工作原理。如果您已经添加了服务引用,请完全删除该服务引用。将服务引用新添加到客户端应用程序并运行该应用程序
使用旧引用名称的原因是,无论何时创建web服务引用,都会创建一个引用文件
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();
}
}
}