2011-08-01 8 views
3

イメージと2つのパラメータを取り込む必要があるWCFサービスを開発中です。 1つはint型であり、もう1つは文字列配列です。画像と一緒に、アップ送信するためにのみ1パラメータなかったのであれば、これは簡単に十分だろう。イメージと複数の引数をアップロードする必要があるRESTful WCFサービス

[OperationContract] 
[WebInvoke(Method = "POST", UriTemplate = "UploadImages/{imageID}")] 
public void UploadImages(int imageID, Stream image) 
{      
} 

さて、このシナリオでは画像がポストのボディです。サービスのコンシューマが3番目のデータを渡す必要がある場合、WCFでどのように見えるのでしょうか?

<system.serviceModel> 
    <client> 
    </client> 
    <bindings> 
     <webHttpBinding> 
     <binding name="webHttpBindingStreamed" transferMode="Streamed"></binding> 
     </webHttpBinding>  
    </bindings> 
    <services> 
     <service name="ImageService"> 
     <endpoint address="" binding="webHttpBinding" behaviorConfiguration="MyWebHttpBehavior" name="ImageServiceWebBinding" contract=IImageService" /> 
     </service> 
    </services> 
    <behaviors>  
     <endpointBehaviors>   
     <behavior name="MyWebHttpBehavior"> 
      <customWebHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <extensions> 
     <behaviorExtensions> 
     <add name="customWebHttp" type="CustomHttpBehaviorExtensionElement, ImageUploader" /> 
     </behaviorExtensions> 
    </extensions> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

答えて

2

以下の例のように、追加パラメータをURIに渡すこともできます。または、それらをHTTPヘッダーとして渡し、WebOperationContext.Current.IncomingRequest.Headersプロパティを使用してフェッチすることもできます。

[OperationContract] 
[WebInvoke(Method = "POST", UriTemplate = "UploadImages/{fileName}?imageId={imageID}")] 
public void UploadImages(int imageID, string fileName, Stream image) 
{      
} 

更新

パラメータの型が配列である場合でも、あなたはまた、クエリ文字列でそれを渡すことができます - しかし、あなたはそのタイプをデコードすることができQueryStringConverterを提供する必要があります。下の例はそれを示しています。

public class StackOverflow_6905108 
{ 
    [ServiceContract] 
    public class Service 
    { 
     [OperationContract] 
     [WebInvoke(Method = "POST", UriTemplate = "UploadImages/{fileName}?array={array}")] 
     public void UploadImages(int[] array, string fileName, Stream image) 
     { 
      Console.WriteLine("Array:"); 
      foreach (var item in array) Console.Write("{0} ", item); 
      Console.WriteLine(); 
     } 
    } 
    public static void SendPost(string uri, string contentType, string body) 
    { 
     HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri); 
     req.Method = "POST"; 
     req.ContentType = contentType; 
     Stream reqStream = req.GetRequestStream(); 
     byte[] reqBytes = Encoding.UTF8.GetBytes(body); 
     reqStream.Write(reqBytes, 0, reqBytes.Length); 
     reqStream.Close(); 

     HttpWebResponse resp; 
     try 
     { 
      resp = (HttpWebResponse)req.GetResponse(); 
     } 
     catch (WebException e) 
     { 
      resp = (HttpWebResponse)e.Response; 
     } 

     Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription); 
     foreach (string headerName in resp.Headers.AllKeys) 
     { 
      Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]); 
     } 
     Console.WriteLine(); 
     Stream respStream = resp.GetResponseStream(); 
     Console.WriteLine(new StreamReader(respStream).ReadToEnd()); 

     Console.WriteLine(); 
     Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* "); 
     Console.WriteLine(); 
    } 
    class MyQueryStringConverter : QueryStringConverter 
    { 
     QueryStringConverter originalConverter; 
     public MyQueryStringConverter(QueryStringConverter originalConverter) 
     { 
      this.originalConverter = originalConverter; 
     } 
     public override bool CanConvert(Type type) 
     { 
      return type == typeof(int[]) || base.CanConvert(type); 
     } 
     public override object ConvertStringToValue(string parameter, Type parameterType) 
     { 
      if (parameterType == typeof(int[])) 
      { 
       return parameter.Split(',').Select(x => int.Parse(x)).ToArray(); 
      } 
      else 
      { 
       return base.ConvertStringToValue(parameter, parameterType); 
      } 
     } 
    } 
    public class MyWebHttpBehavior : WebHttpBehavior 
    { 
     protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription) 
     { 
      return new MyQueryStringConverter(base.GetQueryStringConverter(operationDescription)); 
     } 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "").Behaviors.Add(new MyWebHttpBehavior()); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     SendPost(baseAddress + "/UploadImages/a.txt?array=1,2,3,4", "application/octet-stream", "The file contents"); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

あなたのお返事ありがとうございましたcarlosfigueira。私は、HTTPヘッダーに落ちなければならないと思います。なぜなら、3番目のデータは上に行く必要があるからです。 –

+0

私は更新された答えで追加したので、配列のクエリ文字列パラメータも使用できます。 – carlosfigueira

+0

優秀!手伝ってくれてどうもありがとう! –

関連する問題