使用 WCF RESTFul 服务完整工作示例上传图像

本文介绍了使用 WCF RESTFul 服务完整工作示例上传图像的处理方法,对大家解决问题具有一定的参考价值

问题描述

作为一个相当大的应用程序的一部分,我需要提供一个上传图像的 REST 服务.由于整个应用程序是 .NET,我一直试图找到一个 WCF RESTFul 服务的工作示例,它可以做到但不能(它定义了整个配置).网上有一些例子,但我不能(由于我缺乏 WCF 知识)使它们都不起作用.(我不断抛出异常).

As part of a pretty large application I need to provide a REST Service that uploads an image. Since the whole application is .NET, I've been trying to find an working example of a WCF RESTFul Service that does that but couldn't (Which defines the whole configurations). There are handful of examples online but I couldn't (thanks to my lack of knowledge in WCF) make none of them work. (I keep getting exceptions thrown).

我很乐意与工作人员建立联系.

I'll be happy to get linked to a working one.

推荐答案

请在下面找到示例:

在服务器上:

界面:

[WebInvoke]
string PostImage(Stream stream);

我的实现类:

public string PostImage(Stream stream)
        {
            byte[] buffer = new byte[10000];
            stream.Read(buffer, 0, 10000);
            FileStream f = new FileStream("C:tempsample.jpg", FileMode.OpenOrCreate);
            f.Write(buffer, 0, buffer.Length);
            f.Close();
            stream.Close();
            return "Recieved the image on server";
        }

注意:确保您的 WCF 服务应用程序池标识对 c:Temp 位置具有写入权限以写入文件.或者,您可以替换将图像保存到数据库的逻辑或您希望的任何其他方式.

NOTE: Make sure that your WCF Service application app pools identity has write access to the c:Temp location to write the file. Alternatively you can replace the logic to save the image to db or any other way you want it to.

我在服务器上的 web.config:

My web.config on server:

<services>
      <service name="XMLService.Service1" behaviorConfiguration="default">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="RestBinding" name="Service1" contract="XMLService.IService1" />
      </service>
</services>
<behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
  <serviceBehaviors>        
    <behavior name="default">
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<webHttpBinding>
<binding name="RestBinding">
     <readerQuotas maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" />
     <security mode="None">            
     </security>
     </binding>
</webHttpBinding>

现在在我的客户端上,我有以下代码:

Now on my client i have the following code:

var client = new RestClient();
client.BaseUrl = "http://localhost/XMLService/Service1.svc";
var request = new RestRequest(method) { DateFormat = DataFormat.Xml.ToString(), Resource = resourceUrl };
if(requestBody !=null)
      request.AddParameter("objCompositeType", requestBody);
request.AddFile("stream", image, "Array.png");    
var response = client.Execute(request);
string responseString;
if (response.StatusCode == HttpStatusCode.OK)
{
     responseString = HttpUtility.HtmlDecode(response.Content);
}
else
{
     responseString = response.StatusDescription + " --------------------" + HttpUtility.HtmlDecode(response.Content);
}

我正在使用第三方工具 RestSharp 将我的请求发布到 REST 服务.

I am using a 3rd party tool RestSharp to post my request to the REST Service.

我在 fiddler 中的请求如下所示:

My request in fiddler looks as shown:

POST http://localhost/XMLService/Service1.svc/PostImage HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp 102.0.0.0
Host: rajeshwin7
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299
Content-Length: 2300
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

-------------------------------28947758029299
Content-Disposition: form-data; name="stream"; filename="download.jpg"
Content-Type: application/octet-stream

�����JFIF����������C�       
!#'**%%+7,$3)1-,*4696A),,5���C  


6$$555,,)404,)6,51,5),)+)),,0,.044,),1),)),)))42,,,0,���"��"������������������.�������!1"AQ2a�#q�B�Rr���������������������������������1�����?��4��������������������ՇOF��¼�(�%�az�ROw����l"���Z��g}J�qWR��ʜR�K)�;���<&���G��kw���K���):1���������`��B;e(�:u"����.���_��QS�iB)�2�w�{��Q���k�%�e�xU�^�!Bw~��b�)����-,���e��=k���s��v���}�    ���$�}2}���o�>5N��Ҟ����YхL���T��)=��|�gu�B^ѕͽ&�:st���J�8ae�/�O-%��I���
ES��}�$��MI7',nK8̟xG���m��F��ʤ�K�g)�n^=�#ǎ1�����{P��
��U5)8��儞��8k��N�f����8�ʧu�S�M9��I�K8M�xY���������������j�EZӖ�[Y�O�ۓ(t�ޥH�W�K2�~�M�cn�����M_�����&�K��g������夳�l;��}%A�if�f��Q�MB{1�xofs�u�sX�qsW���T��)V���ʦ'��<���饎S���Y�t
�=�i�R�:nJsm�U̜T���-���,�6U��s��Nڞ���j�_�r�5OӜ��8�%������������������V���N��as���>_<J��v�:p�BݸO��A�&㕞q���>L}N���a{����տX�U�Ӣ�C���~�XϜe�]���3NrWZ���M(F�kd%'��{0���4�����U}M]J����ue���ia%��?���+K��n]��i���n����Mgң,����S��~�W�޶V�)m�95ы�Rm��q��ı��P�
m5_wjQ�ҩ��m,.��Rry^��o��}�O�y^9�N��MT�G��2�9G���+>7=^�^��J4��1�%%���*YMK�Z�s�5 )��Y�U�y:pj۹:*S�mU%�y���24��I)-�qĖ$�����C]�J�4�(�����⛬��d����g�fv�;R�԰��yO�+������������������([Z�VЎT�R�Jks�K���8Ͽ
�4�Wv6�m�6��K�y^y
��u������E��������'E�Pꗯu���ģ�
M5������8b�5{�ҧV�IB26��o̤���O���<�Ԛ�GVR��g�7��,<���<��|���Q��T���CRx�ӏ�>1��V���uj<�7'�r�O������������������)B֜�S�Y�ħ�����z�P�8W�$��n~�S�M�?u�<��������������������]����/�:j+n"���I��ܟ�?��,���'F�c�ytҕ%&�4��k9��0�����������������������i�[FP���9֡����7����,���h�S��_d����������������������%V�XSuj턽1sQ����yϟu��yŕ�i�)Е*�IŶ�W��Y�
&���K�H  ��������������������K�q
zRV�8�#:p���krk>�>�-�='�Ժ{��U}�e�
�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
-------------------------------28947758029299--

当我执行上述请求的 POST 时,我得到以下响应:

And when i perform a POST of the above request i get the below response:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 105
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
Set-Cookie: ASP.NET_SessionId=fdbg45tergdsfg434t; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 27 Feb 2012 12:05:32 GMT

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Recieved the image on server</string>

这篇关于使用 WCF RESTFul 服务完整工作示例上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1159

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1060

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:800

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:887

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:925

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:873

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:855

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:825

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1692

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1298