2016-08-08 3 views
0

スクリーンショットでは、多くのことが明らかになります。基本的には、私はこのサービスを持っており、WCFテストクライアントを使用して、文字列を返すことができます。しかし、Visual Studioを使用して作成したClient Sideリファレンスには、私のメソッドがvoidとして返されることが記載されています。WCF .svcファイルに戻り値の型があります。クライアントコードはすべて無効です。

Test()メソッドのServerConnectionオブジェクトは、Visual Studioによって作成されたサービスリファレンス変数を参照しています。

Root.svc.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace chatService_SignalRService.Services 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Root" in code, svc and config file together. 
    // NOTE: In order to launch WCF Test Client for testing this service, please select Root.svc or Root.svc.cs at the Solution Explorer and start debugging. 
    public class Root : IRoot 
    { 
     public String DoWork() 
     { 
      return "Hello Bailey!"; 
     } 
    } 
} 

IRoot.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace chatService_SignalRService.Services 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRoot" in both code and config file together. 
    [ServiceContract] 
    public interface IRoot 
    { 
     [OperationContract] 
     String DoWork(); 
    } 
} 

私は汎用性のあるクラスライブラリを使用しています。そのクラスライブラリを選択し、サービス参照を追加し、アドレスをWebサーバーにポイントし、ServerCTという名前を付けます。

ポータブルクラスライブラリの下にある私のProfile.csでは、オブジェクトとメソッドを作成します。

using Microsoft.AspNet.SignalR.Client; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Classes 
{ 
    public class Profile : User 
    { 

     public static ServerCT.RootClient ServerConnection = new ServerCT.RootClient(); 


     public void Test() 
     { 
      ServerConnection.DoWorkAsync(); 
     } 
    } 
} 

スクリーンショット:彼らは非同期であり、何の結果を返さないので、 enter image description here

enter image description here

+0

スクリーンショットを掲示する代わりに完全なコードをここに掲載してください。あなたに答えを与えるためにスクリーンショットからあなたのコードを再入力したい人はいません。 –

+0

私はコードを投稿します、多分いくつかの異なるファイルに5行のコードがあります。 –

答えて

1

生成された非同期メソッドは、常に無効です。代わりに、あなたは結果を処理するためのCompletedイベントを使用する必要があります。

ServerConnection.DoWorkCompleted += (sender, args) => 
{ 
    Console.WriteLine($"DoWork result: {args.Result}"); 
}; 
ServerConnection.DoWorkAsync(); // void, the result is provided through event handler 

あなたは同期呼び出しを行うと、同期結果を取得したい場合は、あなたがこのような同期方法DoWork呼び出す必要があります:

ServerConnection.DoWork(); // returns string 

一部のプロジェクトまたは\および構成では、同期操作が生成されないことに注意してください。たとえば、Silverlightアプリケーションの場合。

+0

Visual StudioではDoWork()を呼び出すオプションは提供されず、DoWorkAsync()のみが呼び出されます。 –

+0

おそらく、それは何らかの形で実装または設定に関係しているでしょう。サービスの実装を表示できますか? –

+0

私は移植可能なクラスライブラリを持っています。そのクラスライブラリにはサービスリファレンスがあります。私はそのサービス参照のオブジェクトを作成し、DoWorkAsync()呼び出しを行います。 DoWork()メソッドはクライアント側で指定されていません。 –

関連する問題