0

WCFに関していくつか質問があります。-プログラムはクライアントとサーバーの両方として機能できますか?-私のコードの何が問題になっていますか:

サービス:

[ServiceContract]
public interface IShout
{
    [OperationContract]
    String Broadcast(String message);
}

実装:

public class eveShout : IShout
{
    public String Broadcast(String message)
    {
        return message + " reply";
    }
}

私はcontructorの形式でサービスを開始します:

ServiceHost s = new ServiceHost(typeof(IShout));
s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
s.Open();

、別のフォームのボタンをクリックすると、メッセージを送信して返信を受け取りたいです。私は次のコードを使用します:

ChannelFactory<IShout> channel = new ChannelFactory<IShout>(new BasicHttpBinding(), "http://localhost:9189");
IShout shout = channel.CreateChannel();

String reply = shout.Broadcast("Test");

注:すべてのコードは同じ名前空間にあります。注:最初に「サーバー」(開いている)を起動してから、アプリを続行します。

コードを実行すると、サーバーが作成されます。netstat -aを使用して、ポートが開いているかどうかを確認します。コマンドを実行すると、9189がリスニング状態になっています。ただし、コードはコマンドreply = shout( "test")で停止します。そして私は言う例外を取得します

00:00:59以降の応答を待機している間の要求チャネルのタイムアウト...

4

2 に答える 2

0

はい、アプリをクライアントとサーバーの両方として機能させることができます。

修正が必要なことがいくつかあります。まず、OperationContractを追加してみてください。

[ServiceContract]
public interface IShout
{
    [OperationContract]
    String Broadcast(String message);
} 

次に、インターフェイスではなく、クラスのタイプを取得します。

ServiceHost s = new ServiceHost(typeof(eveShout));
s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
s.Open(); 

名前空間にアクセスする権限があることを確認してください(s.Open()が例外をスローしない場合は、例外をスローする必要があります)。

net http add urlacl url=http://+:9189/ user=...

これらの提案が役立つかどうかを確認してください。

(そうそう、クラスでBroadcastを公開する)

簡単な例のWindowsFormsApplicationは次のようになります...

// form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChannelFactory<IShout> channel = new ChannelFactory<IShout>(new BasicHttpBinding(), "http://localhost:9189");
            IShout shout = channel.CreateChannel();

            String reply = shout.Broadcast("Test"); 

        }
    }
}

// and Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ServiceHost s = new ServiceHost(typeof(eveShout));
            s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
            s.Open(); 

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public class eveShout : IShout
    {
        public String Broadcast(String message)
        {
            return message + " reply";
        }
    } 

    [ServiceContract]
    public interface IShout
    {
        [OperationContract]
        String Broadcast(String message);
    } 
}

これが機能するのと同じくらい簡単なものを手に入れることができるかどうかを確認してください。それは少なくともあなたにそれができること、そして問題がどこかにあることを証明するでしょう。

于 2012-08-12T09:52:46.237 に答える
0

WCFデバッグを有効にします。

これを行う最も簡単な方法は、WCFサービス構成エディターを使用することです。ユーティリティを開き、参照してアプリケーションの構成ファイルを開きます。診断セクションで、[トレースを有効にする]をクリックするだけです。デフォルトのトレースで問題ありません。

アプリケーションを実行すると、フレームワークはログファイルを構成ファイルで指定された場所にダンプします。ダブルクリックして開き、赤いイベントを読み通します(これらは例外または予期しない結果をもたらすイベントです)。これは非常に役立ち、問題が発生している場所を特定するのに役立ちます。

于 2012-08-12T09:58:52.113 に答える