2010-03-26 81 views
1

我有一个WCF服务,我试图通过控制台应用程序连接到测试(尽管将移动到最终接口的WPF),但WCF服务返回void。我已经生成的代理,在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; 
    } 
} 
+1

请告诉我们该方法的签名。也就是说,向我们展示方法名称,返回类型和参数以及在它们上设置的任何属性。 – 2010-03-26 11:21:57

+0

@John - 我已经添加了代码envolved按要求。 – Unflux 2010-03-26 12:22:50

+0

你也可以发布'SupportStaffServiceClient'类的生成代码吗? – 2010-03-26 12:36:04

回答

2

我从未见过svcutil生成方法与ref参数之前 - 它是如何做到的?无论如何,它看起来像你想调用上面的方法在生成的代码 - 这一个需要并返回一个TutorMessage对象。所以,你可以做

TutorObject returnedTutor = client.AddTutor(inputTutorObject); 

或者,它看起来如果调用的ref参数的方法,因为你目前正在做这样的“回报”值放入这些相同的ref参数。所以你通过timet1,请致电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 
+0

感谢您的回复Graham。关于我如何设法生成参考参数,我不知道。 svcutil /语言:cs /out:generatedProxy.cs /config:app.config http:// localhost:1234 关于如何访问返回的值,如果我理解你正确,但我不能做tutorReturn = client.AddTutor(ref时间,参考t1);你是说返回的导师会自动分配给t1对象? – Unflux 2010-03-26 14:31:57

+0

@Abs:我已经添加了一些代码给我的答案,希望这会让事情更清晰。由于输入和返回类型相同,可能是由ref参数产生的方法?我不确定。 – 2010-03-26 14:38:30

+0

你建议使用的方法有一个返回值没有访问修饰符,所以它会默认为私有,我有类似的问题,我显然不是唯一的,发生了什么事情,有任何想法吗? – 2010-07-22 11:08:20

0

我也有类似的问题,我们会forgottent对我们的响应类标记的自定义MessageContractMessageBodyMemberAttribute我们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工具生成一个隐藏了自定义消息合同的细节,只是代码后以我的服务的代理中的字符串返回值的形式返回Response。

0

我有外部服务相同的问题(无法更改它的符号)。我已经添加了/ mc参数给svcutil - 它有帮助。

相关问题