これは、WCF では非常に基本的な質問かもしれません。私はそれに慣れていないので、これを解決する方法がわかりません。
Visual Studio から WCF サービス アプリケーション テンプレートを取得しました。
**インターフェイス: IService1.cs **
using System.ServiceModel;
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
}
** サービス ファイル名 : Service1.svc **
using System.ServiceModel;
namespace WcfService1
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
そして、web.configファイルを次のように編集しました
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="mexBehaviour" name="WcfService1.IService1">
<endpoint address="Service1" binding="basicHttpBinding" contract="WcfService1.IService1"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
エラーは次のとおり です。サービスの追加に失敗しました。サービス メタデータにアクセスできない場合があります。サービスが実行中で、メタデータを公開していることを確認してください。
詳細に:
Error: Cannot obtain Metadata from http://localhost:59790/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:59790/Service1.svc Metadata contains a reference that cannot be resolved: 'http://localhost:59790/Service1.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:59790/Service1.svc. The client and service bindings may be mismatched. The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error URI: http://localhost:59790/Service1.svc The HTML document does not contain Web service discovery information.
Visual Studio によって生成された元の Webconfig ファイル
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
これは単純な構成ファイルの問題であると確信しています。それを理解する方法がわからない。事前にご協力いただきありがとうございます。
Web.config を変更して netTcpBinding Binding を追加すると、WCF testClient でエラーがスローされます。
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="mexBehaviour" name="WcfService1.Service1">
<endpoint address="Service1" binding="basicHttpBinding" contract="WcfService1.IService1"/>
<endpoint address="Service1" binding="netTcpBinding" contract="WcfService1.IService1"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/"/>
<add baseAddress="net.tcp://localhost:8090/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration><br>------------------------------------------------------------------
プロジェクト 2: コンソール アプリでホストされる BasicHttp および NetTcp バインディング
私は以下のweb.configファイルを持っています(すべてを一目で確認できるように、もう一度コピーしただけです)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HelloService.HelloService" behaviorConfiguration="mexBehaviour">
<endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>
<endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:59084/"/>
<add baseAddress="net.tcp://localhost:59076/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
およびホスティング用のコンソール アプリ コード
using System.ServiceModel;
namespace HelloServiceHost
{
class Program
{
static void Main()
{
using(ServiceHost host = new ServiceHost(typeof(HelloService.HelloService)))
{
host.Open();
Console.WriteLine("Host Started");
Console.ReadLine();
}
}
}
}
エラー : host.Open() メソッドでエラーが発生します。
HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
- ポート 8080 が占有されている可能性があると考えて、53895 などの他のポート番号を試しました。運がない!!
- このエラーを参照したときに、アカウントが管理者ではないため、この問題を知りました。私の疑問は、WCFTest Client も私のアカウントで実行されていることです。どうすれば同様のコードを実行できますか?
- また、この作業を行うための提案は高く評価されます。また Webconfig と何か関係があるのでしょうか??
インターフェイスを拡張するクラス
using System.ServiceModel;
namespace HelloService
{
public class HelloService : IHelloService
{
public string GetMessage(string Name)
{
return "Hello " + Name;
}
}
}
インターフェース :
namespace HelloService
{
[ServiceContract]
public interface IHelloService
{
[OperationContract]
String GetMessage(String Name);
}
}
前もって感謝します!!