2013-01-16 3 views
2

ici j'essaye de renvoyer une image du service de wcf. Code de IService1.cscomment retourner une image du service wcf?

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] 
public interface IService1 
{ 

    [OperationContract] 
    [WebGet] 
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
    Stream GetImage(int width, int height); 

} 

Code de Service1.cs

namespace SecondService 
    { 
    public class Service1 : IService1 
    { 
    public Stream GetImage(int width, int height) 
    { 
     // Although this method returns a jpeg, it can be 
     // modified to return any data you want within the stream 
     Bitmap bitmap = new Bitmap(width, height); 
     for (int i = 0; i < bitmap.Width; i++) 
     { 
      for (int j = 0; j < bitmap.Height; j++) 
      { 
     bitmap.SetPixel(i, j, (Math.Abs(i - j) < 2) ? Color.Blue : color.Yellow); 
      } 
     } 
     MemoryStream ms = new MemoryStream(); 
     bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
     ms.Position = 0; 
     System.ServiceModel.Channels.TcpTransportBindingElement transport = new 
     System.ServiceModel.Channels.TcpTransportBindingElement(); 
     transport.TransferMode = TransferMode.Streamed; 
     WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; 
     return ms; 
    } 
    } 
    } 

c'est le code de ma page d'appel, web.config

<system.serviceModel> 
<bindings> 

    <wsHttpBinding> 
    <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00" 
     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
     maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
     textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 


     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
     maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
     <reliableSession ordered="true" inactivityTimeout="00:10:00" 
     enabled="false" /> 
     <security mode="Message"> 
     <transport clientCredentialType="Windows" proxyCredentialType="None" 
      realm="" /> 
     <message clientCredentialType="Windows" negotiateServiceCredential="true" 
      algorithmSuite="Default" /> 
     </security> 
    </binding> 
    </wsHttpBinding> 
</bindings> 

<client> 
    <endpoint address="http://localhost:8732/Design_Time_Addresses/SecondService/Service1/" 
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" 
    contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"> 

    <identity> 
     <dns value="localhost" /> 
    </identity> 
    </endpoint> 
</client> 

quand je appeler le service, je fais face à l'erreur suivante - Le type de contenu image/jpe g du message de réponse ne correspond pas au type de contenu de la liaison (application/soap + xml; jeu de caractères = utf-8). Si vous utilisez un encodeur personnalisé, assurez-vous que la méthode IsContentTypeSupported est implémentée correctement.

je suis nouveau dans le service WCF, je besoin d'aide je suis face à partir de 48 heures et je l'apprécie si quelqu'un fournissent des liens utiles pour beginners.Thanks WCF

+0

Avez-vous vérifié ceci: http://stackoverflow.com/questions/5243929/wcf-service-client-the-content-type-text-html- charset-utf-8-of-the-response-me – Ujjwal

Répondre

0

Vous ne pouvez pas retourner un flux, mais vous devez image encode en tant que tableau d'octets en utilisant un code comme ceci:

ImageSourceConverter oConverter = new ImageSourceConverter(); 
BitmapSource oImageSource = (BitmapSource)oConverter.ConvertFromString(sResourceName); // You 
resource name 

Byte[] oResult; // This is you byte array 

using (MemoryStream oStream = new MemoryStream()) 
{ 
    oEncoder.Frames.Add(BitmapFrame.Create(oImageSource)); 
    oEncoder.Save(oStream); 

    oResult = new byte[oStream.Length];  
    oStream.Position = 0; 

    oStream.Read(oResult, 0, (int)oStream.Length); 
    oStream.Close(); 
} 
+0

En fait, vous pouvez retourner un flux: http://msdn.microsoft.com/en-us/library/ms789010.aspx – eestein

+0

Oh oui, je n'ai pas la fonctionnalité . THX! –