【问题标题】:Can not find the right WCF endpoint configuration找不到正确的 WCF 端点配置
【发布时间】:2014-08-19 07:30:13
【问题描述】:

我创建了从 android 获取数据并将它们保存到 SQL 中的服务。我正在使用 IIS 7

我的代码:

namespace WcfService_SuiviColis
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom d'interface "IService1" à la fois dans le code et le fichier de configuration.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat= WebMessageFormat.Json,  BodyStyle =WebMessageBodyStyle.Wrapped,  UriTemplate = "SaveData")]
        void SaveData(Pers_Ordre_NET oData);      
    }

    [DataContract]
    public class Pers_Ordre_NET
    {
        [DataMember]
        string _CodeClient;
        public string CodeClient
        {
            get { return _CodeClient; }
            set { _CodeClient = value; }
        }

       [DataMember]
        string _CodeDest;
        public string CodeDest
        {
            get { return _CodeDest; }
            set { _CodeDest = value; }
        }

        [DataMember]
        string _NoOrdre;
        public string NoOrdre
        {
            get { return _NoOrdre; }
            set { _NoOrdre = value; }
        }

        [DataMember]
        string _DateTampon;
        public string DateTampon
        {
            get { return _DateTampon; }
            set { _DateTampon = value; }
        }

        [DataMember]
        string _GeoPos;
        public string GeoPos
        {
            get { return _GeoPos; }
            set { _GeoPos = value; }
        }

        [DataMember]
        string _StsOrdre;
        public string StsOrdre
        {
            get { return _StsOrdre; }
            set { _StsOrdre = value; }
        }

        [DataMember]
        string _Camion;
        public string Camion
        {
            get { return _Camion; }
            set { _Camion = value; }
        }
    }
}

service.svc.cs:

namespace WcfService_SuiviColis
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom de classe "Service1" dans le code, le fichier svc et le fichier de configuration.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        public void SaveData(Pers_Ordre_NET oOrdre)
        {
            try
            {
                using (var connectionWrapper = new Connexion())
                {
                    var connectedConnection = connectionWrapper.GetConnected();
                    string sql_Adding = "INSERT INTO [SUIVI_ORDRE]"+
                                          " ([CODE_CLIENT] ,[CODE_DEST],[NO_ORDRE],[DATE_TAMPON],[GPS_POS],[STATUS_ORDRE],CAMION)"+
                                    "VALUES (@CODE_CLIENT,@CODE_DEST,@NO_ORDRE,@DATE_TAMPON,@GPS_POS,@STATUS_ORDRE,@CAMION)";
                    SqlCommand comm_Insrt = new SqlCommand(sql_Adding, connectionWrapper.conn);
                    comm_Insrt.Parameters.AddWithValue("@CODE_CLIENT", oOrdre.CodeClient);
                    comm_Insrt.Parameters.AddWithValue("@CODE_DEST", oOrdre.CodeDest);
                    comm_Insrt.Parameters.AddWithValue("@NO_ORDRE", oOrdre.NoOrdre);
                    comm_Insrt.Parameters.AddWithValue("@DATE_TAMPON", oOrdre.DateTampon);
                    comm_Insrt.Parameters.AddWithValue("@GPS_POS", oOrdre.GeoPos);
                    comm_Insrt.Parameters.AddWithValue("@STATUS_ORDRE", oOrdre.StsOrdre);
                    comm_Insrt.Parameters.AddWithValue("@CAMION", oOrdre.Camion);
                    comm_Insrt.ExecuteNonQuery();                   
                }
            }
            catch (Exception excThrown)
            {
                throw new Exception(excThrown.Message);
            }
        }    
    }   
}

web.config:

<system.serviceModel>  
    <services>          
        <service name="WcfService_SuiviColis.Service1" behaviorConfiguration="ServiceBehaviour">
            <endpoint 
                address="SaveData" 
                behaviorConfiguration="httpBehavior"
                binding="webHttpBinding" 
                contract="WcfService_SuiviColis.IService1"  />
        </service>
    </services>  
    <behaviors> 
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">       
          <serviceMetadata httpGetEnabled="true"/>          
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="httpBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
  </system.serviceModel>
  <system.webServer>
     <security>
         <requestFiltering>
             <verbs>
                 <add verb="POST" allowed="true"/>
             </verbs>
             <fileExtensions>
                 <add fileExtension=".svc" allowed="true"/>
             </fileExtensions>
         </requestFiltering>
     </security>

     <directoryBrowse enabled="true"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

我找不到端点的正确配置。

当我这样写时:

<endpoint 
    address="SaveData" behaviorConfiguration="httpBehavior" 
    binding="webHttpBinding" 
    contract="WcfService_SuiviColis.IService1" />

找不到端点

当我这样写时:

<endpoint 
    address="" behaviorConfiguration="httpBehavior"
    binding="webHttpBinding" 
    contract="WcfService_SuiviColis.IService1"  />

我的方法不允许

当我这样写时:

<endpoint 
    address="" 
    binding="basicHttpBinding"   
    contract="WcfService_SuiviColis.IService1"  />

我收到错误 415,类型不匹配,因为我想接收 JSON,但在提琴手中我得到了 html

我也输入了Factory="System.ServiceModel.Activation.WebServiceHostFactory"

当我放

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

找不到错误 400 页面

我这样称呼我的方法:

http://mydomain:4004/Code/WcfService_SuiviColis/WcfService_SuiviColis/Service1.svc/SaveData

已编辑 我认为正确的端点:

<endpoint 
    address="" behaviorConfiguration="httpBehavior"
    binding="webHttpBinding" 
    contract="WcfService_SuiviColis.IService1"  />

但是对于这个端点,我得到了错误 405,不允许使用 mmethode。 IIS 7 可能不允许 POST(从 ANDROID 接收数据到服务器)? 因为我用 GET 创建了另一个 wcf 程序(将数据从服务器发送到 ANDROID)它工作正常。

【问题讨论】:

    标签: web-services wcf endpoint


    【解决方案1】:

    乍一看,您定义的端点地址无效。它应该包含服务所在机器的完整地址以及端口号

    <endpoint address ="http://mydomain:4004/...../>
    

    【讨论】:

    • 感谢您的回复,我做到了,但我收到了错误 500,因为 ,然后我将其更改为 false,但我收到错误 400 页面未找到
    【解决方案2】:

    由于您使用的是 IIS,所以要使用的 Web URL 基本上由 IIS 定义:您的服务器名称、虚拟目录的名称、*.svc 文件的路径和位置。

    http://YourWebServer/VirtualDirectory/Path/service.svc
    

    然后,您的端点定义中可能有一个额外的相对地址,所以如果您使用

    <endpoint 
        address="SaveData" behaviorConfiguration="httpBehavior" 
        binding="webHttpBinding" 
        contract="WcfService_SuiviColis.IService1" />
    

    那么完整的 URL 将是:

    http://YourWebServer/VirtualDirectory/Path/service.svc/SaveData
    

    我不确定您是否需要向该 URL 添加另一个 /SaveData,因为您已将其定义为服务合同中的 UriTemplate

    http://YourWebServer/VirtualDirectory/Path/service.svc/SaveData/SaveData
                                                           ++++++++ ********
                                                              |         |
                                                 relative address       |
                                                 from endpoint          |
                                                                        |
                                              UriTemplate from your service contract
    

    未找到端点通常意味着您使用错误的 URL 来尝试访问您的服务。

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2014-10-06
      相关资源
      最近更新 更多