簡體   English   中英

解決WCF中的服務問題

[英]Problem addressing a service in WCF

我正在使用wcf編寫服務。 我用兩個項目創建了一個解決方案:

  • 庫:一個項目,用於存儲有關服務的文件(包含該服務的接口和相應的實現)。 該項目是一個圖書館項目。
  • 托管應用程序用於托管這些服務的項目(自我托管)。 由於這個原因,該項目是具有配置文件的可執行項目,我在其中放置了用於配置服務所需的信息。

我還寫了一個客戶來打電話給該服務。 這將稱為“ 客戶端應用程序”

我有服務 以下是界面和實現(圖書館項目):

namespace EchoWcfLibrary {
    /// <summary>
    /// The interface specifies for those classes implementing it (services), the operation that the service will expose.
    /// </summary>
    [ServiceContract]
    public interface IService1 {
        // This does not use serialization (implicit serialization in considered: base types used).
        [OperationContract]
        string GetData(int value);
        // This uses data contracts and serialization.
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

    [DataContract]
    public class CompositeType {
        // Members not serialized
        bool boolValue = true;
        string stringValue = "Hello ";
        // Serialized
        [DataMember]
        public bool BoolValue {
            get { return boolValue; }
            set { boolValue = value; }
        }
        // Serialized
        [DataMember]
        public string StringValue {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

以下是服務主機應用程序(可執行項目)的啟動:

namespace WcfServiceApplication {
    public static class Program {
        static void Main(string[] args) {
            // Setting endpoints and setting the service to start properly.
            // Base address specified: http://localhost:8081/Service1
            Console.WriteLine("Beginning...");
            using (ServiceHost host = new ServiceHost(typeof(Service1), new Uri("http://localhost:8081/Service1"))) {
                Console.WriteLine("Opening host...");
                host.Open();
                Console.WriteLine("Waiting...");
                System.Threading.Thread.Sleep(1000000);
                Console.WriteLine("Closing...");
                host.Close();
                Console.WriteLine("Quitting...");
            }
        }
    }
}

以下是可執行項目(托管應用程序)中的App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
    <system.serviceModel>
        <services>
            <service name="WcfServiceLibrary.Service1">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8081/Service1" />
                    </baseAddresses>
                </host>
                <!-- Service Endpoints -->
                <!-- Unless fully qualified, address is relative to base address supplied above -->
                <endpoint address="/GoInto/Svc" 
                                    binding="basicHttpBinding" 
                                    contract="WcfServiceLibrary.IService1">
                    <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="/GoInto/Sav"
                                    binding="basicHttpBinding"
                                    contract="WcfServiceLibrary.IService1">
                    <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <!-- Metadata Endpoints -->
                <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
                <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
                <endpoint address="GoInto/Mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>

        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
                    <serviceMetadata httpGetEnabled="True"/>
                    <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="False" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>

</configuration>

以下是客戶端可執行項目中的程序啟動(已鏈接到庫項目),基本上這是客戶端:

namespace WcfServiceClient {
    class Program {
        static void Main(string[] args) {
            ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IService1)), new BasicHttpBinding(), new EndpointAddress("http://localhost:8081/Service1"));
            ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>(httpEndpoint);
            IService1 svc = channelFactory.CreateChannel();
            Console.WriteLine(svc.GetData(121));
            System.Threading.Thread.Sleep(10000);
        }
    }
}

好吧...我的問題如下:此應用程序有效!!! 為什么有問題呢??? 問題是,在托管服務時,我在App.config文件中指定了三個終結點:兩個basicHttp和一個元數據終結點。 好吧,我想解決<endpoint address="/GoInto/Svc"...終結點,該終結點具有完整的地址(請注意,我已經指定了基本地址): http://localhost:8081/Service1/GoInto/Svc

好吧,不幸的是,在客戶端中,我尋址了這個端點: http://localhost:8081/Service1 ,這僅僅是基地址……為什么這樣做? 我想在客戶端中指定此地址:

namespace WcfServiceClient {
    class Program {
        static void Main(string[] args) {
            ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IService1)), new BasicHttpBinding(), new EndpointAddress("http://localhost:8081/Service1/GoInto/Svc"));
            ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>(httpEndpoint);
            IService1 svc = channelFactory.CreateChannel();
            Console.WriteLine(svc.GetData(121));
            System.Threading.Thread.Sleep(10000);
        }
    }
}

但是,如果我這樣做,則會引發不匹配錯誤:

由於EndpointDispatcher上的AddressFilter不匹配,因此無法在接收方處理帶有To'http:// localhost:8081 / Service1 / GoInto / Svc'的消息。 檢查發送方和接收方的EndpointAddresses是否一致。

為什么不起作用?

基址必須在ServiceHost構造函數或元素中的一個位置中指定。 如果您在兩個地方都有,則WCF會拋出一個異常,指出您具有同一方案(HTTP)的兩個基地址。

可能發生的情況是您在托管項目的app.config上的服務名稱不匹配,因此未選擇配置(您得到的是默認終結點,該終結點的地址與該終結點的地址相同)。基本地址)。 嘗試在托管代碼上添加foreach循環,您應該看到服務正在偵聽的端點的地址。

                Console.WriteLine("Opening host...");
            host.Open();

            foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
            {
                Console.WriteLine("Endpoint:");
                Console.WriteLine("   Address: {0}", endpoint.Address.Uri);
                Console.WriteLine("   Binding: {0}", endpoint.Binding);
                Console.WriteLine("   Contract: {0}", endpoint.Contract.Name);
            }

            Console.WriteLine("Waiting...");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM