【问题标题】:How can you receive an image sent from Android using WCF?如何使用 WCF 接收从 Android 发送的图像?
【发布时间】:2019-06-01 16:00:02
【问题描述】:

如果您有一个简单的示例说明如何使用 WCF 接收图像并将该图像保存在自定义文件夹中,它们将为我节省大量时间并引导我走上正确的道路。

我看到你可以使用 Stream 类型或 byte[] 类型,但是我不能正确地做到这一点。

非常感谢您的宝贵时间。

【问题讨论】:

    标签: c# android wcf


    【解决方案1】:

    我做了一个demo,希望对你有用。
    服务器(WCF服务应用)

        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            [WebInvoke(Method ="POST",RequestFormat =WebMessageFormat.Json,ResponseFormat =WebMessageFormat.Json)]
            Task UploadStream(Stream stream);
    
        }
        public class Service1 : IService1
        {
            public async Task UploadStream(Stream stream)
            {
                using (stream)
                {
                    //save file to local folder
                    using (var file=File.Create(@"C:\"+Guid.NewGuid().ToString()+".png"))
                    {
                        await stream.CopyToAsync(file);
                    }
                }
            }
    }
    

    Web.config(wcf 配置)

    <system.serviceModel>
        <services>
          <service name="WcfService3.Service1">
            <endpoint address="" binding="webHttpBinding" contract="WcfService3.IService1" bindingConfiguration="mybinding" behaviorConfiguration="rest"></endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
          </service>
        </services>
        <bindings>
          <webHttpBinding>
            <binding name="mybinding" receiveTimeout="00:30:00" sendTimeout="00:30:00" maxReceivedMessageSize="104857600" transferMode="Streamed">
              <security mode="None"></security>
            </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
    
          <endpointBehaviors>
            <behavior name="rest">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    为了创建流传输模式服务,我们也可以使用BasicHttpBinding创建soap服务。
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/02733eae-a871-4655-9a2b-0ca1095b07ea/problems-when-uploading-a-stream-to-wcf?forum=wcf
    在客户端,您可以使用第三方库来调用 WCF REST 服务,例如 ksoap。但您也可以使用 HttpClient 库发送 http 请求。就像下面的代码sn-ps(HttpClient是.Net库,不是java库,但是用法差不多)。

    class Program
    {
        static void Main(string[] args)
        {
            HttpClient client = new HttpClient();
            HttpContent content = new StreamContent(File.OpenRead(@"2.png"));
            Task.WaitAll(client.PostAsync("http://10.157.18.36:8800/service1.svc/UploadStream", content));
            Console.WriteLine("OK");
        }
    }
    

    如果有什么我可以帮忙的,请随时告诉我。

    【讨论】:

    • 其实这个demo就是一个很好的例子,因为它给出了异步的方法,非常感谢分享
    【解决方案2】:

    您想使用来自 WCF 端点的图像响应吗? 这里有一个例子: https://www.dotnetcurry.com/wcf/723/download-files-using-wcf-rest-endpoints

    【讨论】:

    • 其实就是下一步了,谢谢分享。然而,这是另一种方式,Android 应用程序将向 WCF 服务发送一个图像文件,这就是我卡住的地方
    猜你喜欢
    • 2015-02-05
    • 2015-08-16
    • 1970-01-01
    • 2012-09-26
    • 2015-01-08
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多