2015-02-11 166 views
-1

你好,我正在尋找很長時間,但每個答案都沒有解決我的問題。我有REST服務Wcf REST服務和客戶

namespace WcfService1 
{ 

[ServiceContract] 
public interface IRestServiceImpl 
{ 
    [OperationContract] 
    [WebInvoke(Method = "GET", 
     ResponseFormat = WebMessageFormat.Xml, 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "images")] 
    List<MyImage> getImages();   
} 
[DataContract] 
public class MyImage 
{ 
    public MyImage(string name, Image img) 
    { 
     Name = name; 
     Img = img; 
    } 
    [DataMember] 
    public string Name 
    { 
     get; 
     set; 
    } 
    [DataMember] 
    public Image Img 
    { 
     get; 
     set; 
    } 
} 
} 

這裏是實現:

namespace WcfService1 
{ 

public class RestServiceImpl : IRestServiceImpl 
{ 
    public List<MyImage> getImages() 
    { 
     List<MyImage> images = new List<MyImage>(); 
     string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.jpg"); 

     foreach (string file in files) 
     { 
      Image im = Image.FromFile(file); 
      images.Add(new MyImage(file, im)); 
     } 
     return images; 
    } 
} 
} 

的Web.Config:

<?xml version="1.0"?> 
    <configuration> 
     <system.web> 
     <compilation debug="true" targetFramework="4.0" /> 
     </system.web> 
     <system.serviceModel> 
     <services> 
     <service name="WcfService1.RestServiceImpl" behaviorConfiguration="ServiceBehaviour"> 
     <endpoint address="" binding="webHttpBinding" contract="WcfService1.IRestServiceImpl" behaviorConfiguration="web">   
     </endpoint> 
     </service> 
     </services> 
     <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehaviour"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
     <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
     </behaviors> 
     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
     <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
    </configuration> 

接下來,我創建客戶端應用和增值業務參考和選擇參考從本地主機上宣佈

有代碼的客戶端

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     ServiceReference1.RestServiceImplClient service = new ServiceReference1.RestServiceImplClient(); 

     ServiceReference1.MyImage[] images = service.getImages(); 
     service.Close(); 
     List<ServiceReference1.MyImage> files = images.Select(im => new ServiceReference1.MyImage() { Img = im.Img, Name = im.Name }).ToList(); 

     listViewImages.ItemsSource = files; 

    } 
} 

在此行中:

ServiceReference1.MyImage[] images = service.getImages(); 

我有例外:

There was no endpoint listening at http://localhost:55008/RestServiceImpl.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

當我看着我的InnerException有:

The remote server returned an error: (404) Not Found.

這裏是我的app.config文件對於客戶:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<system.serviceModel> 
    <client> 
    <endpoint address="http://localhost:55008/RestServiceImpl.svc" binding="basicHttpBinding" contract="ServiceReference1.IRestServiceImpl"> 
    </endpoint> 
    </client> 
    <behaviors> 
    <serviceBehaviors> 
    <behavior name="ServiceBehaviour"> 
    <serviceMetadata httpGetEnabled="true"/> 
    <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 
    </behaviors> 
</system.serviceModel> 
</configuration> 

我知道的話題是非常受歡迎的,但所有的答案couldn`t幫助我。

回答

0

從已發佈的代碼,服務web.config中沒有與basicHttpBinding的端點。要通過代理使用服務,我相信你需要在你的問題中添加一個帶有BasicHttpBinding的終端作爲客戶端web.config。它確實公開了一個REST端點。要調用REST服務,可以使用下面的url來調用。

http://localhost:55008/RestServiceImpl.svc/images 
0

嘗試在客戶端代碼中添加REST服務的Web引用而不是服務引用。