How to invoke WCF service method which is deployed at some other server from webapi controller in any application Approach-1

(In order to Enlarge images Please Right Click and Open images in New Tab).


Step 1)

Let’s see created WCF service



namespace SP.Web.Services
public interface IPatientsJSONService
List<PatientDTO> GetPatientsBySearchExtendedCriteria(PatientSearchDTO searchCriteria);

PatientEntityDTO GetPatientEntityById(Guid patientId);
public class PatientsJSONService : IPatientsJSONService
private log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string _org = ConfigurationManager.AppSettings[“crm.organizationName”];

[WebInvoke(Method = “POST“, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public List<PatientDTO> GetPatientsBySearchExtendedCriteria([FromBody]PatientSearchDTO searchCriteria)
//Your Logic


step 2) Let see service web.config for it





<service behaviorConfiguration=”webHttpGetMetadata” name=”SP.Web.Services.PatientsJSONService”>
<endpoint binding=”webHttpBinding” contract=”SP.Web.Services.IPatientsJSONService” behaviorConfiguration=”Web” />


Step 3) let’s see how to invoke it from any applications webapi controller

Just observe how parameter is used for service method




public HttpResponseMessage Post([FromBody]PatientSearchNetworkParams postDTO)
HttpResponseMessage resp;
string orignalContent = “”;
PatientSearchDTO patientSearchDTO = new PatientSearchDTO();
string param= JsonConvert.SerializeObject(patientSearchDTO , new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat });
string result = string.Empty;
var request = (HttpWebRequest)WebRequest.Create(http://YourserverName/ISV/;
if (request != null)
request.ContentType = “application/json”;
request.Method = “POST”;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] requestBodyBytes = GetBytes(param);
request.ContentLength = requestBodyBytes.Length;
System.IO.Stream postStream = request.GetRequestStream();
postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
var response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
System.IO.Stream responseStream = response.GetResponseStream();
if (responseStream != null)
var reader = new System.IO.StreamReader(responseStream);

result = reader.ReadToEnd();
resp = Request.CreateResponse(HttpStatusCode.OK, result );
return resp;




Solution for: HTTP Error 404.3 – Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler.

(In order to Enlarge images Please Right Click and Open images in New Tab).


I fixed on Windows 10:

  1.  Control Panel – Programs and Features – Turn Windows features on or off.
  2. Select .NET Framework 4.6 Advanced Services – WCF Services
  3. Check HTTP Activation.
  4. OK done.
  5. Please find below screenshot.


how to replace “\” with “_” in string in

(In order to Enlarge images Please Right Click and Open images in New Tab).



Let’s see how to replace “\” with “_” in string in

Since it’s not straight forward by using String.Replace method.

So let’s take move ahead with it

suppose your string item is like that
string filepath=”C:\\\\DemoFolder\\Claims”;

now use below code

filepath.Replace(@”\”, @”_”);

Output=>  “C:__DemoFolder_Claims”


How to check File/Directory Access for particular user and allow that user to read/Write into that directory.

(In order to Enlarge images Please Right Click and Open images in New Tab).



Let’s see How to check File/Directory Access for particular user and allow that user to read/Write into that directory.


In order to do this perform below steps



Add this class in your project with name NetworkConnection


public class NetworkConnection : IDisposable
string _networkName;

public NetworkConnection(string networkName,
NetworkCredential credentials)
_networkName = networkName;

var netResource = new NetResource()
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName

var result = WNetAddConnection2(

if (result != 0)
//throw new Win32Exception(result, “Error connecting to remote share”);


public void Dispose()

protected virtual void Dispose(bool disposing)
WNetCancelConnection2(_networkName, 0, true);

private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);

private static extern int WNetCancelConnection2(string name, int flags,
bool force);

Step-2) also add this class as well NetResource

public class NetResource
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;

public enum ResourceScope : int
Connected = 1,

public enum ResourceType : int
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,

public enum ResourceDisplaytype : int
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b



Let us see how to use it in order to check access for particular user with domain name and user  credentials:

NetworkCredential theNetworkCredential = new NetworkCredential(“username”, “password”, “domainName”);

//Here NetworkCredential  it’s from System.Net;

Now use it like

using (new NetworkConnection(FilePath , theNetworkCredential))

//your code goes here where you can perform operations on File
//FilePath should be with filename into it

byte[] fileData = Convert.FromBase64String(FileBinaryData);
Stream stream = new MemoryStream(fileData);

//Below code is for writing into file
using (new NetworkConnection(FilePath , theNetworkCredential))
File.WriteAllBytes(FilePath + item.FileName, fileData);

using (Stream file = File.Create(FilePath , fileData.Length, FileOptions.WriteThrough))


//Below code is for reading from file
byte[] fileBinary = null;
fileBinary = File.ReadAllBytes(FilePath);

//Use this fileBinary for rendering on UI




using ( new Impersonator( "YourUsername", "YourDomainname", "YourPassword" ) )
   //Here your all code goes which is going to perform I/O on
   // Files/Directories
//Which avoids all access path exceptions


add this class




How to download any kind of files with angularJs

(In order to Enlarge images Please Right Click and Open images in New Tab).



Let’s see How to download any kind of files with angularJs






Just we need to use contentType  in proper format

if (fileName.indexOf(“xls”) > -1 || fileName.indexOf(“xlsx”) > -1) {
var contentType = ‘application/octet-stream’;
} else if (fileName.indexOf(“pdf”) > -1) {
var contentType = ‘application/pdf’;
} else {
var contentType = ‘application/octet-stream’;
var fileDownLoadName = fileName;
var def = $q.defer();
$ + “/portalDocumentDownload”, angular.toJson(helper), { responseType: ‘arraybuffer’ },
{ headers: { ‘Content-Type’: ‘application/json’ } })
.success(function (data, status, headers, config) {
var ieEDGE = navigator.userAgent.match(/Edge/g);
var ie = navigator.userAgent.match(/.NET/g); // IE 11+
var oldIE = navigator.userAgent.match(/MSIE/g);
var blob = new window.Blob([data], {
type: contentType
if (ie || oldIE || ieEDGE) {
window.navigator.msSaveBlob(blob, fileDownLoadName);
else {
var file = new Blob([data], { type: contentType });
var fileURL = URL.createObjectURL(file);
var a = document.createElement(‘a’);
a.href = fileURL; = ‘_blank’; = fileDownLoadName;

read all file names exists in perticular directory

(In order to Enlarge images Please Right Click and Open images in New Tab).




Let’s see how to read all file names exists in perticular directory




string downloadpath = @”C:\\1″;
DirectoryInfo dir = new DirectoryInfo(downloadpath);
var files = dir.GetFiles(“*.*”);
foreach (var file in files)
FilesInfo obj = new FilesInfo();
obj.FileName = file.Name;