Big memory consumption during wcf file transfer - c#

I'm using this example and although the transfering method is streamed once I hit upload the ram consumption on the client jumps to 1GB. After the transfer is finished the memory isn't reduced. My code is below:
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
if (File.Exists(openFileDialog1.FileName))
{
//System.IO.FileInfo fileInfo = new System.IO.FileInfo(openFileDialog1.FileName);
FileTransferServiceReference.ITransferService clientUpload = new FileTransferServiceReference.TransferServiceClient();
FileTransferServiceReference.RemoteFileInfo uploadRequestInfo = new RemoteFileInfo();
uploadRequestInfo.FileName = openFileDialog1.FileName;
uploadRequestInfo.Length = new FileInfo(openFileDialog1.FileName).Length;
uploadRequestInfo.FileByteStream = File.OpenRead(openFileDialog1.FileName);
clientUpload.UploadFile(uploadRequestInfo);
}
}
on the App.config there is
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.web>
<httpRuntime maxRequestLength="2097150"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ITransferService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://62.1.193.176/transferTEST/wcfTest.TransferService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITransferService"
contract="FileTransferServiceReference.ITransferService" name="BasicHttpBinding_ITransferService" />
</client>
</system.serviceModel>
</configuration>

Related

Consuming Web Service in console C#

I'm trying to consume a WebService in a console application, but I keep getting this error.
'Unable to load endpoint configuration section for
'Giftcard.WebServiceSoap' contract. Found more than one configuration
for the contract. Indicate the preferred endpoint configuration
section by name.'
This is my code:
public class Program
{
static void Main(string[] args)
{
UserWebService.WebServiceSoapClient webServiceSoapClient = new UserWebService.WebServiceSoapClient();
string id = "12345543";
webServiceSoapClient.Login(id);
}
}
WEB Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WebServiceSoap">
<security mode="Transport" />
</binding>
<binding name="WebServiceSoap1" />
</basicHttpBinding>
<customBinding>
<binding name="WebServiceSoap12">
<textMessageEncoding messageVersion="Soap12" />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://localhost:444/WebService.asmx" binding="basicHttpBinding"
bindingConfiguration="WebServiceSoap" contract="UserWebService.WebServiceSoap"
name="WebServiceSoap" />
<endpoint address="https://localhost:444/WebService.asmx" binding="customBinding"
bindingConfiguration="WebServiceSoap12" contract="UserWebService.WebServiceSoap"
name="WebServiceSoap12" />
</client>
</system.serviceModel>
</configuration>
I've added the reference to the webservice from here:
Add the bindingConfiguration="WebServiceSoap" name you want to use in the constructor of UserWebService.WebServiceSoapClient("WebServiceSoap").
public class Program
{
static void Main(string[] args)
{
UserWebService.WebServiceSoapClient webServiceSoapClient = new UserWebService.WebServiceSoapClient("WebServiceSoap");
string id = "12345543";
webServiceSoapClient.Login(id);
}
}

How to get data from Business Central?

I have made a C#-project where I am trying to connect to my Business Central cloud sandbox.
In Business Central, I have made an extension with a new codeunit, which has a function that I call "MHSTest". It just returns a text.
In Visual Studio, I have made the service reference that connects to the SOAP URL in Business Central. So I can see the name of my function, "MHSTest", in Visual Studio.
When I write this in Visual Studio, I get an error, because it tries to connect anonymously:
var client = new MHSTest.CSharpCodeunit_PortClient();
myTextBox.Text = client.MHSTest();
If I try the below instead, I get an error message saying that "https" is not valid. It has to be "http".
string endpoint = "">api.businesscentral.dynamics.com/.../CSharpCodeunit";
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.MaxBufferSize = 999999;
binding.MaxReceivedMessageSize = 999999;
var client = new MHSTest.CSharpCodeunit_PortClient(binding, new EndpointAddress(endpoint));
myTextBox.Text = client.MHSTest();
How do I connect to Business Central and get (or set) data?
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Debitorer_Binding">
<security mode="Transport" />
</binding>
<binding name="Debitorer_Binding1" />
<binding name="CSharpCodeunit_Binding">
<security mode="Transport" />
</binding>
<binding name="CSharpCodeunit_Binding1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="api.businesscentral.dynamics.com/.../2999befa-1255-4304-a79a-67de0e24f090/Sandbox/WS/CRONUS%20Danmark%20A%252FS/Page/Debitorer?tenant=msneua5672t92712555&aid=FIN"
binding="basicHttpBinding" bindingConfiguration="Debitorer_Binding"
contract="BCCustomer.Debitorer_Port" name="Debitorer_Port" />
<endpoint address="api.businesscentral.dynamics.com/.../2999befa-1255-4304-a79a-67de0e24f090/Sandbox/WS/CRONUS%20Danmark%20A%252FS/Codeunit/CSharpCodeunit?tenant=msneua5672t92712555&aid=FIN"
binding="basicHttpBinding" bindingConfiguration="CSharpCodeunit_Binding"
contract="MHSTest.CSharpCodeunit_Port" name="CSharpCodeunit_Port" />
</client>
</system.serviceModel>
</configuration>
The problem is solved. I just needed to add this code after defining my client variable:
System.ServiceModel.Security.UserNamePasswordClientCredential cre = client.ClientCredentials.UserName;
cre.UserName = " ";
cre.Password = " ";
Use the Export-NAVData cmdlet to export data from a Business Central database. You can export company-specific data, and you can choose to include global data, application data, and application objects. When you export data from a Business Central database, the data is stored in a file with the extension .

C# WCF Error on change Endpoint

I'm doing a service to comunicate only for test and learning,
but I'm trying to change the URL Endpoint and its returning a error
n: There was no endpoint listening at http://servername:8732/TestService/ that could accept the message. This is often caused by an incorrect address or SOAP action.
That is my code:
private void button1_Click(object sender, EventArgs e)
{
var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.None;
var myEndPointAdress = new EndpointAddress("http://servername:8732/TestService/");
MyCalculatorServiceClient service = new MyCalculatorServiceClient(myBinding, myEndPointAdress);
MessageBox.Show(Convert.ToString(service.soma(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text))));
}
App.config "Edited":
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyCalculatorService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://servername:8732/TestService/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyCalculatorService"
contract="ServiceReference1.IMyCalculatorService" name="BasicHttpBinding_IMyCalculatorService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

Embed app.config containing service info inside the application executable

My app.config is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ESDServiceSoapBinding">
<security mode="Transport" />
</binding>
<binding name="ESDServiceSoapBinding1">
<security mode="Transport" />
</binding>
<binding name="ESDServiceSoapBinding2" />
<binding name="ESDServiceSoapBinding3" />
<binding name="ESDServiceSoapBinding4">
<security mode="Transport" />
</binding>
<binding name="ESDServiceSoapBinding5" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://downloadswebregr.mydomain.com:443/ESDServiceWeb/services/ESDEntitlementServiceAPI"
binding="basicHttpBinding" bindingConfiguration="ESDServiceSoapBinding1"
contract="ESDRegService.ESDEntitlementServiceAPI" name="ESDEntitlementServiceAPI" />
<endpoint address="http://eotwasqa1.mydomain.com:19081/ESDServiceWeb/services/ESDEntitlementServiceAPI"
binding="basicHttpBinding" bindingConfiguration="ESDServiceSoapBinding3"
contract="ESDBackupService.ESDEntitlementServiceAPI" name="ESDEntitlementServiceAPI1" />
<endpoint address="https://downloadsweb.mydomain.com:443/ESDServiceWeb/services/ESDEntitlementServiceAPI"
binding="basicHttpBinding" bindingConfiguration="ESDServiceSoapBinding4"
contract="ESDBackupService.ESDEntitlementServiceAPI" name="ESDEntitlementServiceAPI2" />
</client>
</system.serviceModel>
</configuration>
I tried embedding this inside the executable using the build action "Embedded Resource" but then my application is not able to read the services information. I want to get rid of it as I want to ship only a single file. i.e. the executable.
I searched for similar questions on stackoverflow and people say, it should be outside so that it can be configured or modified from outside.
Please note that, I dont want it to get changed from outside and just want to embed it.
The basic purpose of the configuration file is to allow the external users to configure something in your application.Hence the configuration files needs to be distributed to the end users.If you intend not to allow changes to the end points ,it has to created inside your code as suggested in the previous comment.
Check the following link.
Create WCF endpoint configurations in the client app, in code?

WCF Service Consume issue

I have a windows service that hosts a WCF service. I have a client that consumes the service. The client does not recognize the ServiceReference1 even though I added it as a Service Reference.
I have been trying to fix this all day - I have read the code multiple times - I have used WCF before with no issues (in the same manner as this).
Interface
namespace ValidStateService
{
[ServiceContract]
public interface IValidStateWCFService
{
// Add two numbers
[OperationContract(IsOneWay = false)]
int AddNumbers(int numA, int numB);
}
}
Implementation of the Interface
namespace ValidStateService
{
public class ValidStateWCFService : IValidStateWCFService
{
// Call the agent to add two numbers and return the result
public int AddNumbers(int numA, int numB)
{
try
{
return 20; // Always return 20 for testing
}
catch (Exception ex)
{
return -1;
}
}
}
}
Windows Service App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ValidStateService.ValidStateWCFService">
<endpoint address="" binding="basicHttpBinding" contract="ValidStateService.IValidStateWCFService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/ValidStateService/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Now on a Winform i have added reference to the service but servicereference1 is not recognized.
Code to add two numbers
*Winform App.config*
private void buttonGetDataFromAgent_Click(object sender, EventArgs e)
{
try
{
InstanceContext context = new InstanceContext(this);
ServiceReference1. // Winform App does not recognize this i.e no intellisense
??????
Client App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8732/ValidStateService/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IValidStateWCFService"
contract="ServiceReference1.IValidStateWCFService" name="BasicHttpBinding_IValidStateWCFService" />
</client>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IValidStateWCFService" />
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>

Categories

Resources