Van*_*nel 3 c# rest wcf web-services visual-studio-2010
我正在使用C#开发WCF .NET Framework 4.0.
我用这个Visual Studio模板创建了这个WCF:

我需要发送带有两个或三个参数的图像.这就是OperationContract我所拥有的(我要求最后一个):
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "orders/")]
OrderContract[] allOrders();
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "filteredOrders/")]
OrderContract[] GetOrders(IdsMessage msg);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "completeFilteredOrders/")]
OrderContract[] LoadCompleteFilteredOrders(IdsMessage msg);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "saveEReports/")]
Boolean SaveEReports(EReportContract[] eReports);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "saveEReport/")]
long SaveEReport(EReportContract eReport);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "UploadPhoto/{eReportId}/{imageType}")]
Boolean UploadPhoto(string eReportId, string imageType, Stream fileContents);
}
Run Code Online (Sandbox Code Playgroud)
这是Web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="EReportService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="EReportService.IRestServiceImpl" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding maxReceivedMessageSize="2097152" maxBufferSize="2097152" transferMode="Streamed"/>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<connectionStrings>
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
当我运行服务时,我得到以下异常:
该操作必须具有单个参数,其类型为Stream
如果我这样做:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "UploadPhoto")]
Boolean UploadPhoto(Stream fileContents);
Run Code Online (Sandbox Code Playgroud)
它工作得很好但我需要用图像发送更多数据.
此服务适用于Android平板电脑应用程序.以下代码显示了我现在如何将图像发送到服务器:
public static Boolean sendImage(String url, String filePath)
{
try
{
MultiValueMap<String, Object> formData;
Resource resource = new FileSystemResource(filePath);
// populate the data to post
formData = new LinkedMultiValueMap<String, Object>();
formData.add(OrderSpringController.FILE, resource);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
// Sending multipart/form-data
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
// Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
HttpEntity<MultiValueMap<String, Object>> requestEntity =
new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate(true);
restTemplate.getMessageConverters().add(messageConverter);
// Make the network request, posting the message, expecting a String in response from the server
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,
Boolean.class);
// Return the response body to display to the user
return response.getBody();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
浏览到元数据端点(http:// localhost:2351/RestServiceImpl.svc)时发生错误.
如何发送图像和参数?
如果切换到(BasicHttp或WSHttp)绑定,那么您可以通过组合单个数据类型来实现您的目标,包括通过属性和Stream对象本身的所有自定义参数.然后,您将使用消息传递样式而不是RPC对话.并请注意[MessageContract]系列的WCF DTO装饰器.
[ServiceContract]
public interface ITransferService
{
[OperationContract]
RemoteFileInfo DownloadFile(DownloadRequest request);
[OperationContract]
void UploadFile(RemoteFileInfo request);
}
[MessageContract]
public class DownloadRequest
{
[MessageBodyMember]
public string FileName;
}
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
Run Code Online (Sandbox Code Playgroud)
}
这有必要的代码http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP
但我强烈建议WCF:使用消息合约流媒体和http://blogs.msdn.com/b/carlosfigueira/archive/2011/03/25/wcf-streaming-inside-data-contracts.aspx
这是必须的,以及:)
| 归档时间: |
|
| 查看次数: |
18327 次 |
| 最近记录: |