【发布时间】: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