繁体   English   中英

使用C#通过Web服务(SOAP)以XML显示图像实体?

[英]Display Image entity in XML through webservice(SOAP) using C#?

有一个Web应用程序,其中几乎没有实体,包括图像实体。该图像已存储在DB中,现在我必须通过Web服务访问它并以XML显示。 尝试将图像实体转换为byte []以及String base64。 但是没有解决。 请帮助我解决此问题。 提前致谢。

在此处输入图片说明

您是否面临将图像传回的问题?

我将图像转换为base64并以字符串形式返回。 一切正常。 您在那里遇到了什么问题。

但是这种方法需要更新WCF服务以及客户端中的maxReceivedMessageSize参数。

副作用:增加maxReceivedMessageSize存在一些问题。 更多的内存占用空间,可能会打开DDOS攻击。

服务合同

    public class ImageService : IImageService
    {
        public string GetImageData(string path)
        {
            if (File.Exists(path))
            {
                return ConvertFile(path);
            }
            else
                return string.Empty;
        }

        private string ConvertFile(string path)
        {
            using (Image image = Image.FromFile(path))
            {
                using (MemoryStream m = new MemoryStream())
                {
                    image.Save(m, image.RawFormat);
                    byte[] imageBytes = m.ToArray();

                    // Convert byte[] to Base64 String
                    string base64String = Convert.ToBase64String(imageBytes);
                    return base64String;
                }
            }
        }        
    }

服务配置

<system.serviceModel>
    <bindings>
      <basicHttpsBinding>
        <binding name="basicHttp" allowCookies="true"
                 maxReceivedMessageSize="20000000" 
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
            <readerQuotas maxDepth="32" 
                 maxArrayLength="200000000"
                 maxStringContentLength="200000000"/>
        </binding>
      </basicHttpsBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

客户代码

    static void Main(string[] args)
    {
        ImageService.ImageServiceClient client = new ImageService.ImageServiceClient();
        string data = client.GetData(@"C:\Users\v-sridve\Pictures\Travel_Error_New.PNG");
    }

客户端配置

<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" maxReceivedMessageSize="1000000" maxBufferSize="1000000" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:7541/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="ImageService.IService1"
                name="BasicHttpBinding_IService1"  />
        </client>
    </system.serviceModel>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM