2017-12-03 15 views
1

最近WCFで作業を開始しましたが、問題解決の手掛かりがありません。 サービスホストを使用してWCFサービスを開始しますが、ブラウザでURIを使用するとサービスの契約が表示されず、ChannelFactoryを使用して接続しようとすると例外が発生します。WCFエラー - サービスエンドポイントを取得できません

私はVisual Studio 2017でプロジェクトを作成しましたが、configファイルには何もしなかったので、ベースアドレスを変更しました。サービスインタフェースと実装の両方がルートプロジェクトの "フォルダ"にあり、ファイアウォールやウイルス対策を無効にしようとしましたが、何も動作していないようです。

App.configファイル:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name=""> 
        <serviceMetadata httpGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <services> 
      <service name="TaskExecutor.Exec"> 
       <endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec"> 
        <identity> 
         <dns value="localhost" /> 
        </identity> 
       </endpoint> 
       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
       <host> 
        <baseAddresses> 
         <add baseAddress="http://localhost:8001/TaskExecutor/Exec/" /> 
        </baseAddresses> 
       </host> 
      </service> 
     </services> 
    </system.serviceModel> 
</configuration> 

サービス・インターフェース:

namespace TaskExecutor 
{ 
    [ServiceContract] 
    public interface IExec 
    { 
     [OperationContract] 
     void DoWork(); 
    } 
} 

サービスの実装:

namespace TaskExecutor 
{ 
    public class Exec : IExec 
    { 
     public void DoWork() 
     { 
      Console.WriteLine("Doing work."); 
     } 
    } 
} 

プログラム起動サービス:

using (ServiceHost host = new ServiceHost(typeof(Exec))) 
{ 
    host.Open(); 
    Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]); 
} 

Console.WriteLine("Press any key to end..."); 
Console.ReadLine(); 

プログラムはメッセージを表示し起動した後:

EndpointAddress endpoint = new EndpointAddress("http://localhost:8001/TaskExecutor/Exec/"); 
BasicHttpBinding binding = new BasicHttpBinding(); 

ChannelFactory<IExec> channelFactory = new ChannelFactory<IExec>(binding, endpoint); 
IExec proxy = channelFactory.CreateChannel(); 

proxy.DoWork(); 

そして、それは例外与える::

exec service started at http://localhost:8001/TaskExecutor/Exec/ 
Press any key to end... 

サービスクライアントコードは次のようである

System.ServiceModel.EndpointNotFoundException occurred 
    HResult=0x80131501 
    Message=There was no endpoint listening at http://localhost:8001/TaskExecutor/Exec/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. 

Inner Exception 1: 
WebException: Unable to connect to the remote server 

Inner Exception 2: 
SocketException: No connection could be made because the target machine actively refused it 
私は真剣にドン

」を何をすべきかを知っており、どんな助けも素晴らしいだろう。

ありがとうございました!

+0

は、WCFにステップのチュートリアルで簡単なステップのために、このサイトにアクセスしてください - – MethodMan

+0

おかげ入力用https://www.tutorialspoint.com/wcf/wcf_creating_service.htm、それは多くの助けにはなりませんでしたが、私はそのチュートリアルにあるほとんど同じことをやってきました – gqmartins

答えて

0

あなたはメタデータを公開していますが、サービスにバインドしていません。

<behaviors> 
      <serviceBehaviors> 
       <behavior name="metadadiscovery"> 
        <serviceMetadata httpGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 

この動作をサービスにバインドします。

<services> 
     <service name="TaskExecutor.Exec" behaviorConfiguration="metadadiscovery"> 
      <endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec"> 
      <identity> 
         <dns value="localhost" /> 
        </identity> 
       </endpoint> 
       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
       <host> 
        <baseAddresses> 
         <add baseAddress="http://localhost:8001/TaskExecutor/Exec/" /> 
        </baseAddresses> 
       </host> 
      </service> 
     </services> 

ブラウザにアドレスを入力すると、wsdlが表示されます。 http://localhost:8001/TaskExecutor/Exec/

+0

入力いただきありがとうございます!しかし、まだそれはしません、私はあなたの答えを使用して同じエラーが発生し続ける – gqmartins

+0

wsdlを閲覧することはできますか? –

+0

ベースURIの前に?wsdlを置くか、ベースURIを使用する場合でも、何もせず、エラーを返します。それがあなたのマシンで動作するように管理していますか? – gqmartins

0

私は何が間違っているのか分かりました。サービスを開始するコードでした。代わりに私が持っているもののそれは次のようになります。

using (ServiceHost host = new ServiceHost(typeof(Exec))) 
{ 
    host.Open(); 
    Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]); 
    Console.WriteLine("Press any key to end..."); 
    Console.ReadLine(); 
} 
関連する問題