我有一个WCF服务,我试图通过控制台应用程序进行连接以进行测试(尽管将移动到WPF以获得最终接口)。我已经生成了代理,在visual studio中为我的项目添加了服务引用,我可以看到我在WCF界面中创建的所有方法:
SupportStaffServiceClient client = new SupportStaffServiceClient("WSHttpBinding_ISupportStaffService");
client.myMethod(message);
但是当我调用一个方法时,该方法在WCF接口中被指定为返回一个值,该方法在控制台应用程序中返回void。
client.getMethod(message);
WCF服务方法肯定会返回一条消息,我只是不确定为什么客户端无法“看到”返回。
[服务代码]
[ServiceContract(Namespace="http://localhost/supportstaff")]
public interface ISupportStaffService
{
[OperationContract]
TutorMessage AddTutor(TutorMessage message);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class SupportStaff : ISupportStaffService
{
private ITutorService tutors;
public SupportStaff()
{
// Create the binding
WSHttpBinding logBind = new WSHttpBinding();
// Create the channel factory to the logging service
ChannelFactory<ILogger> logFactory = new ChannelFactory<ILogger>(logBind, "http://localhost:8000/logger");
// Create the connection to the logging service
this.logger = logFactory.CreateChannel();
// Create the binding
WSHttpBinding tutorBind = new WSHttpBinding();
// Create the channel factory to the Tutor service
ChannelFactory<ITutorService> tutorFactory = new ChannelFactory<ITutorService>(tutorBind, "http://localhost:8001/tutors");
// Create the connection to the Tutor service
this.tutors = tutorFactory.CreateChannel();
}
TutorMessage ISupportStaffService.AddTutor(TutorMessage message)
{
// First log that we have received an add Tutor message
// Create a log message
LogMessage logMessage = new LogMessage();
logMessage.Time = message.Time;
logMessage.Message = "[Supprt Staff Service] Add Tutor message received";
// Send the log message to the logging service
logger.Log(logMessage);
// Create a request to add the Tutor to the Tutor service
TutorMessage request = new TutorMessage();
request.Time = DateTime.Now;
request.Tutor = message.Tutor;
// Send the add Tutor message to the Tutor message
tutors.AddTutor(request);
// Display the message
Console.WriteLine("Added Tutor " + message.Tutor.Number);
// Log the message
logMessage = new LogMessage();
logMessage.Time = DateTime.Now;
logMessage.Message = "[Support Staff Service] Added Tutor " + message.Tutor.Number;
logger.Log(logMessage);
// Create the return message
TutorMessage toReturn = new TutorMessage();
toReturn.Time = DateTime.Now;
toReturn.Tutor = message.Tutor;
// Return the return message
return toReturn;
}
}
[ServiceContract(Namespace = "http://localhost/tutors")]
public interface ITutorService
{
[OperationContract]
void AddTutor(TutorMessage message);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class TutorService : ITutorService
{
private Dictionary<string, Tutor> tutors = new Dictionary<string, Tutor>();
void ITutorService.AddTutor(TutorMessage message)
{
// First log the fact that we have received an Add message
// Create the log message
LogMessage logMessage = new LogMessage();
logMessage.Time = message.Time;
logMessage.Message = "[Tutor Service] Tutor add message received";
// Send the log message to the logging service
logger.Log(logMessage);
// Now add the new Tutor to the collection of Tutors
tutors[message.Tutor.Number] = message.Tutor;
// Display message that Tutor is added
Console.WriteLine("Added tutor : " + message.Tutor.Number);
// Log the new Tutor
logMessage = new LogMessage();
logMessage.Time = DateTime.Now;
logMessage.Message = "[Tutor Service] Added tutor : " + message.Tutor.Number;
logger.Log(logMessage);
}
}
[客户代码]
class Program
{
static void Main(string[] args)
{
SupportStaffServiceClient client = new SupportStaffServiceClient("WSHttpBinding_ISupportStaffService");
try
{
localhost.tutor.tutor t1 = new localhost.tutor.tutor();
t1.name = "Big Dave";
t1.number = "t123";
DateTime time = DateTime.Now;
client.AddTutor(ref time, ref t1);
localhost.tutor.tutor t2 = new localhost.tutor.tutor();
client.RetrieveTutor(ref time, ref t1);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message.ToString());
}
finally
{
Console.WriteLine("Press <RETURN> to exit");
Console.ReadLine();
}
}
}
[svcutil生成代码]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class SupportStaffServiceClient : System.ServiceModel.ClientBase<ConsoleApplication1.SupportStaffService.ISupportStaffService>, ConsoleApplication1.SupportStaffService.ISupportStaffService {
public SupportStaffServiceClient() {
}
public SupportStaffServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName) {
}
public SupportStaffServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public SupportStaffServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public SupportStaffServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}
public string HelloWorld(string name) {
return base.Channel.HelloWorld(name);
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
ConsoleApplication1.SupportStaffService.TutorMessage ConsoleApplication1.SupportStaffService.ISupportStaffService.AddTutor(ConsoleApplication1.SupportStaffService.TutorMessage request) {
return base.Channel.AddTutor(request);
}
public void AddTutor(ref System.DateTime time, ref ConsoleApplication1.SupportStaffService.tutor tutor) {
ConsoleApplication1.SupportStaffService.TutorMessage inValue = new ConsoleApplication1.SupportStaffService.TutorMessage();
inValue.time = time;
inValue.tutor = tutor;
ConsoleApplication1.SupportStaffService.TutorMessage retVal = ((ConsoleApplication1.SupportStaffService.ISupportStaffService)(this)).AddTutor(inValue);
time = retVal.time;
tutor = retVal.tutor;
}
}
答案 0 :(得分:2)
我以前从未见过svcutil
生成一个带有ref
参数的方法 - 它是如何做到的?无论如何,看起来你想要在生成的代码中调用上面的方法 - 这个方法接受并返回一个TutorMessage
对象。所以你可以做到
TutorObject returnedTutor = client.AddTutor(inputTutorObject);
或者,如果您使用ref
参数调用方法,就像您当前正在执行的那样,“return”值将被放入那些相同的ref参数中。因此,您传入time
和t1
,调用AddTutor
,现在您传入的变量将包含返回的值。所以,你可以做到
t1.name = "Big Dave";
t1.number = "t123";
DateTime time = DateTime.Now;
client.AddTutor(ref time, ref t1);
// time now contains the returned DateTime from the service
// t1 now contains the returned Tutor from the service
答案 1 :(得分:0)
我遇到了类似的问题,我们忘记在我们的响应类上使用MessageContract
标记自定义MessageBodyMemberAttribute
我们的messageResponse类没有公开其数据合同中的任何成员因此调用服务返回无效。
之前...
[MessageContract]
public class MyCustomResponse
{
public string Response { get; set; }
}
...
在...
[MessageContract]
public class MyCustomResponse
{
[MessageBodyMember(Namespace = "http://MyNamespace")]
public string Response { get; set; }
}
将MessageBodyMember
添加到Response成员后,svcutil会生成隐藏自定义Message Contract详细信息的代码,并仅将Response作为字符串返回值从我的服务代理返回。
答案 2 :(得分:0)
我对外部服务有同样的问题(无法改变其表示法)。我已经将/ mc参数添加到svcutil - 它有所帮助。