If you have a simple example of how you can receive an image using WCF and save that image in a custom folder, they would save me a lot of time and guide me on the right track.
I've seen that you can use the Stream type or the byte[] type, but I could not do it correctly.
Thank you very much for your time.
I have made a demo, wish it is useful to you.
Server (WCF service application)
[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 configuration)
<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>
For creating stream transfer mode service, we could also create soap service with BasicHttpBinding.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/02733eae-a871-4655-9a2b-0ca1095b07ea/problems-when-uploading-a-stream-to-wcf?forum=wcf
On client end, you could use third-party library to call the WCF rest service, such as ksoap. But you also could send a http request with HttpClient library. Just like the following code snippets (HttpClient is .Net library, not java library, but the usage is similar).
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");
}
}
Feel free to let me know if there is anything I can help with.
You want to consume an image response from a WCF endpoint?
An example here:
https://www.dotnetcurry.com/wcf/723/download-files-using-wcf-rest-endpoints
Related
I created a webService with Basic Authentication (using this tutorial).
There is my web.config :
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="UsernameWithTransportCredentialOnly">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="RequestUserNameConfig">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="MyInterface.CredentialsChecker,App_Code.CredentialsChecker"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="RequestUserNameConfig" name="MyInterface.MyService">
<endpoint
address="https://localhost:47336/MyService.svc"
binding="basicHttpBinding"
bindingConfiguration="UsernameWithTransportCredentialOnly"
name="BasicEndpoint"
contract="MyInterface.IMyService">
</endpoint>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="false" />
</system.serviceModel>
A folder App_Code contains file CredentialsChecker.cs with this code :
namespace MyInterface
{
public class CredentialsChecker : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
/* ... Some Code ... */
}
}
}
I created a project that I want to use to test my Web Service. But When I want to add the service as service reference, I got the error :
The provided URI scheme 'https' is invalid; expected 'http'.
Did I miss something in my web service ?
Your UsernameWithTransportCredentialOnly is of type basicHttpBinding. So you need to specify an endpoint that supports the binding. Either Change your address to http, or change the binding to wsHttp
<endpoint
address="http://localhost:47336/MyService.svc"
binding="basicHttpBinding"
bindingConfiguration="UsernameWithTransportCredentialOnly"
name="BasicEndpoint"
contract="MyInterface.IMyService">
</endpoint>
I am an android developer. I want to create an android application which fetches the xml response from a WCF web service using soap. I have created a WCF web service, in which it fetches the XML document and return the content of that document. Here I dont know how to consume that content in android. Moreover, I am new to WCF and I am confused whether I am doing it right or wrong. Any help would be appreciated.
IService.cs
namespace WcfXML
{
[ServiceContract (Namespace="http://tempuri.org/")]
public interface IService1
{
[OperationContract, XmlSerializerFormat]
XmlElement GetData();
}
}
Service1.svc
namespace WcfXML
{
public class Service1 : IService1
{
public XmlElement GetData()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"D:\defaultGraphProperties.xml");
return xmlDoc.DocumentElement;
}
}
}
After creating this, when I run(F5), I am not able to invoke the method. How can I consume this web services in android? Please Help!!!!
You need to add configuration for webHttpBinding in web.config under the <Bindings>.
Then add the below code in web.config under the <System.serviceModel>
<services>
<service name="Test_WcfService.Service1" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingConfiguration" behaviorConfiguration="web" contract="Test_WcfService.IService1"></endpoint>
<endpoint address="Service1.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfiguration" contract="Test_WcfService.IService1"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingConfiguration" maxReceivedMessageSize="2147483647" />
</webHttpBinding>
<basicHttpBinding>
<binding name="basicHttpBindingConfiguration" maxReceivedMessageSize="2147483647" />
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- 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>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
After doing this changes in web.config you will be able to consume the wcf service from Browser.
Replace the Test_WcfService.Service1 and Test_WcfService.IService1 with your WCF Service name and interface.
To consume the WCF Service , you will have to do a Soap Request from Android.
Also if possible share your demo wcf service i will make the necessary configuaration changes at my end.
I have a classic WCF webservice. Few weeks ago, in order to answer to a client demand, I added Rest management to my webservice.
But, at the beginning of the week, another client said to me its system can only manage XML-RPC. So he needs to connect to my webservice via this protocol.
So I found this : Configuring XML-RPC behavior for IIS-hosted .SVC file?
First, I compiled the Microsoft.Samples.XmlRpc in order to add it to my project. Two Dll appears : Microsoft.Samples.XmlRpc & Microsoft.ServiceModel.XmlRpc
Then, I created a XmlRpcEndpointBehaviorExtension class, the same as the post above :
namespace WsZendesk
{
public class XmlRpcEndpointBehaviorExtension : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
// this comes from Microsoft.Samples.XmlRpc
return new XmlRpcEndpointBehavior();
}
public override Type BehaviorType
{
get { return typeof(XmlRpcEndpointBehavior); }
}
}
}
After, I created my interface for Xml-Rpc :
namespace WsZendesk
{
[ServiceContract]
public interface IWsZendeskRpc
{
[OperationContract(Action = "wszendesk.GetUserIdFromBarcode")]
void GetUserIdFromBarcode(String sXmlIn, out String sXmlOut);
}
}
Finaly, I modified my web.config in order to allow RPC :
<system.serviceModel>
<services>
<service name="WsZendesk.WsZendesk" behaviorConfiguration="WsZendeskServiceBehavior">
<endpoint address="rest" behaviorConfiguration="restfulBehavior"
binding="webHttpBinding" bindingConfiguration="" name="RESTEndPoint"
contract="WsZendesk.IWsZendeskRest" />
<endpoint address="xmlrpc" behaviorConfiguration="xmlRpcBehavior"
binding="webHttpBinding" bindingConfiguration="" name="RPCEndPoint"
contract="WsZendesk.IWsZendeskRpc" />
<endpoint address="" behaviorConfiguration=""
binding="basicHttpBinding" bindingConfiguration="" name="SOAPEndPoint"
contract="WsZendesk.IWsZendesk" />
</service>
</services>
<extensions>
<behaviorExtensions>
<add name="xmlRpc"
type="WsZendesk.XmlRpcEndpointBehaviorElement, WsZendesk" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="restfulBehavior">
<webHttp />
</behavior>
<behavior name="xmlRpcBehavior">
<xmlRpc />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WsZendeskServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Visual Studio said he don't know the child element 'xmlRpc'. So, when I try to launch my webservice, the same error appears during the execution.
Anybody can help me to use Xml-Rpc with my existing webservice ?
For information, my project is in C# 4.
It was not this:
<add name="xmlRpc"
type="WsZendesk.XmlRpcEndpointBehaviorElement, WsZendesk" />
But this:
<add name="xmlRpc"
type="WsZendesk.XmlRpcEndpointBehaviorExtension, WsZendesk" />
I have a WCF service with a post method. This takes a single entity.
[OperationContract, FaultContract(typeof(ServiceError))]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped]
Entity SaveEntity(Entity entity);
I have a file with an xml serialised representation of these objects. I need to be able to post this xml to the service directly without deserialising it at the client side (architecture issues, we don't have the references).
Is it possible to do this by constructing a request with something like the HttpClient?
var client = new HttpClient(HttpClient:);
client.PostAsync("http://localhost:55217/MyService.svc/SaveEntity", new HttpContent
{
Headers = new System.Net.Http.Headers.HttpContentHeaders
{
// can I put my serialised xml here?
}
}
Here's the config:
<system.serviceModel>
<services>
<service behaviorConfiguration="Default" name="MyService">
<endpoint address="" binding="webHttpBinding" contract="IMyService" behaviorConfiguration="webBehavior" bindingConfiguration="fullMessageSize" />
<endpoint address="ws" binding="wsHttpBinding" contract="IMyService" bindingConfiguration="fullMessageSize" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="fullMessageSize" maxReceivedMessageSize="104857600" />
</webHttpBinding>
<wsHttpBinding>
<binding name="fullMessageSize" maxReceivedMessageSize="104857600" />
</wsHttpBinding>
</bindings>
</system.serviceModel>
Why not create an intermediate service?
[OperationContract, FaultContract(typeof(ServiceError))]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped]
Entity SaveEntity(XElement entitySerialized)
{
var entity = Deserialize(entitySerialized);
var realService = new MyServiceClient();
return realService.SaveEntity(entity);
}
The intermediate service can have the references required for serialization.
You could consider creating a service-to-service endpoint for use between the intermediate service and the real service. That endpoint could use one of the faster bindings like netTcpBinding, that you might not want to use with your clients.
I am writing a WCF Rest service to return a JSON message. I've been trying to use an example I found on the internet as a guide. Any time I fire up the test client, none of my methods are displayed. Navigating to the Uri while the service is running yields me a "page cannot be displayed" page. Not exactly too sure where to go from here. Any help would be appreciated.
Web Config:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJasonP"
crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="WcfRestLicense.LicenseService"
behaviorConfiguration="WebServiceBehavior">
<endpoint behaviorConfiguration="jsonBehavior"
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJasonP"
contract="WcfRestLicense.ILicenseService" />
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
</service>
</services>
<!--<client />-->
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WebServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add
scheme="http"
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJasonP" />
</protocolMapping>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="false"
multipleSiteBindingsEnabled="true" />
Service method:
public IQueryable<customer> GetCustomerById(string customerId)
{
int custId = Convert.ToInt32(customerId);
return _context.customers.Where(c => c.cust_id == custId);
}
Interface:
[ServiceContract]
public interface ILicenseService
{
[OperationContract]
[WebGet(UriTemplate = "customer/{customerId}/",
RequestFormat= WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
IQueryable<customer> GetCustomerById(string customerId);
}
If by test client you're talking about the WcfTestClient, then it won't work with RESTful services; it's designed to work with SOAP based web services. You can test RESTful services in a browser by passing in the appropriate URI, something like this:
http://<location of your service>/service/1
Where the number 1 would be a customer ID. This is a rough example as a) I don't do a lot with RESTful services and b) I'm not sure what your actual address is.
As far as getting a 404 when you go to the Uri, it sounds like you're looking for the help page. You can enable that in your config file:
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJasonP"
crossDomainScriptAccessEnabled="true"
enableHelp="true" />
</webHttpBinding>
</bindings>