您好我在寻找很长时间,但每个答案都没有解决我的问题。我有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>
接下来,我创建了Client App并添加了Service Reference,并从localhost声明的上层
中选择了引用有客户端代码
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();
我有例外:
http://localhost:55008/RestServiceImpl.svc没有可以接受该消息的端点。这通常是由错误的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在)。
当我查看InnerException时,我有:
远程服务器返回错误:(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>
我知道这个话题非常受欢迎,但所有答案都无法帮助我。
答案 0 :(得分:0)
从您发布的代码中,服务web.config没有带BasicHttpBinding的端点。要通过代理使用服务,我相信你需要在你的问题中根据客户端web.config添加一个带有BasicHttpBinding的端点。它确实暴露了一个REST端点。要调用REST服务,可以使用如下所示的URL调用。
http://localhost:55008/RestServiceImpl.svc/images
答案 1 :(得分:0)
尝试在客户端代码中添加REST服务的Web引用而不是服务引用。