2014-04-18 66 views
2

我已经创建了两种方法之一是获取,另一个是创建在WCF REST服务插入数据表示错误:远程服务器返回一个错误:(404)未找到

对于获取数据工作正常,但是当我试图发布数据显示错误如Method not Found(404)

该代码如下。

合同方法:

public interface IContactPositionService {  
    [OperationContract]  
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET",RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetContactPositionList")]  
    List<ContactPosition> GetContactPositionList(); 
[OperationContract]  
    [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "AddContactPosition")]  
int AddContactPosition(ContactPosition position);} 

和通讯Serivice类是这样的:

public class ContactPositionService : IContactPositionService 
    { 
    public List<ContactPosition> GetContactPositionList() 
     { 
      DataSet ds = clsObj.GetContactPositionsList(); 
      return ConverterHelper.Convert<ContactPosition>(ds.Tables[0]); 
     } 

public int AddContactPosition(ContactPosition position) 
     { 
      // throw new System.NotImplementedException(); 
     } 

而且在Web.config文件我已经配置端点为:

<services> 
     <service name="VirtusMobileService.ContactPositionService" behaviorConfiguration="VMS.Position.ServiceBehaviour"> 
     <endpoint address="" behaviorConfiguration="VMS.Position.EndPointBehaviour" 
      binding="webHttpBinding" bindingConfiguration="" name="VMS.Position.EndPoint" 
      contract="VirtusMobileService.IContactPositionService" /> 
     <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="VMS.Position.EndPointBehaviour"> 
      <webHttp helpEnabled="true" /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="VMS.ServiceBehaviour"> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     <behavior name="VMS.Position.ServiceBehaviour"> 
      <serviceMetadata httpGetEnabled="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

从我们称客户端Asp.net页面为添加联系人对象的服务,如下所示:

private void AddData() 
     { 
      string sURL = "http://localhost:51293/ContactPositionService.svc/AddContactPosition";  
      ContactPosition order = new ContactPosition 
      { 
       PositionCode="10550", 
       PositionFinbaseId=11, 
       PositionTitle="Manager" 
      }; 

      DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ContactPosition)); 
      MemoryStream mem = new MemoryStream(); 
      ser.WriteObject(mem, order); 
      string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length); 
      WebClient webClient = new WebClient(); 
      webClient.Headers["Content-type"] = "application/json"; 
      webClient.Encoding = Encoding.UTF8; 
      webClient.UploadData(sURL, "PUT", mem.ToArray());   // Here it is showing Error 
      Console.WriteLine("Order placed successfully..."); 

} 

虽然涉及到“ uploadData”它显示例如错误“ 远程服务器返回错误:(404)未找到”

If i am trying to Get the Data using the "GetContactPositionList" 

它显示的数据正确,但是当我试图用 “PUT”方法则显示出错误的工作。

我测试在Fiddler中的URL,它显示了同样的错误响应 部首“HTTP/1.1 405不允许的方法”

<div id="content"> 
     <p class="heading1">Service</p> 
     <p xmlns="">Method not allowed. Please see the <a rel="help-page" href="http://localhost:51293/ContactPositionService.svc/help">service help page</a> for constructing valid requests to the service.</p> 
    </div> 

请推荐答案。

谢谢。

+0

你得到405当方法是什么,但PUT和404与PUT?你能否确定你得到的是404而不是400? –

+0

yest这种方法错误是405只有 –

回答

0

在拉琴,你必须指定application/jsonContent Type,它会打服务Try this link

+0

我做到了这一点,但没有用。 –

0

而是 “webClient.UploadData” 的,试试这个.. webClient.UploadString(SURL, “PUT”,数据);

+0

我用过但没用 –

0

我想你可以使用System.ServiceModel.ChannelFactory来让自己更容易。它将大大简化您与服务的交互,因为您将拥有强类型变量,方法名称本身,而不必自己乱搞序列化。

您可以取代你AddData方法:

private void AddData() 
{ 
    // Create the service channel; 'factory' and/or 'svc' can be class members so we can re-use them. 
    var factory = new ChannelFactory<IContactPositionService>(new WebHttpBinding(), new EndpointAddress("http://localhost:51293/ContactPositionService.svc")); 
    factory.Endpoint.Behaviors.Add(new WebHttpBehavior()); 
    var svc = factory.CreateChannel(); 

    ContactPosition order = new ContactPosition 
    { 
     PositionCode = "10550", 
     PositionFinbaseId = 11, 
     PositionTitle = "Manager" 
    }; 

    try 
    { 
     var result = svc.AddContactPosition(order); 
     Console.WriteLine("Order# " + result.ToString() + " placed successfully..."); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.ToString()); 
    } 
} 

注:这需要你调用项目不得不System.ServiceModel.dll和System.ServiceModel.Web.dll参考。此外,呼叫项目将需要知道约IContactPositionServiceContactPosition。我的调用项目与您的WCF项目不同,并且尚未引用您的WCF项目,那么您可以将您的服务接口和对象移动到第三个类库中,然后您可以在WCF主机和任何使用者之间共享它们。

+0

其实我们试图利用REST服务的好处,给客户端类型不是一个好主意。 –

+0

@VGSNaidu你表达了一个根本性的误解。所讨论的类型实际上只需要是服务器的请求和响应结构,而服务器在与服务器通信时已经暴露(实际上是必要的)。 –

相关问题